vendredi 10 avril 2020

Indirect modification of overloaded on Laravel - Relationship

I use Laravel 5 and i have to table: "Link" Table with 7 columns, is this 7 column i have and "image_id" column. "Image" Table, in this table i have two column, an "id" column and a "url" column.

I want to put on my update method the url in the table called "Image" and the id of the url in my "image_id" column of my table "Link".

I have tried this code:

public function update(Request $request, $id)
{
    // Validate the data
    $link = Link::find($id);

    $this->validate($request, array(
            'title' => 'required|max:255',
            'link'  => 'nullable',
            'description'  => 'required'
    ));


    $link = Link::find($id);

    $link->title = $request->input('title');
    $link->link = $request->input('link');
    $link->description = $request->input('description');
    $link->linkImage->url = 'testurl';

    $link->save();

    Session::flash('success', "Le lien à été correctement mis à jour.");

    return redirect()->route('links.index');
}

This is my Link class:

<?php

namespace App\Models\Services;

use Illuminate\Database\Eloquent\Model;

class Link extends Model
{

/**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'service_rubrique_link';

protected $fillable = ['title','description','ordre','image_id'];


public function linkImage() 
{
    return $this->belongsTo('App\Models\Storage\Imageup');
}

}

And my Image Class:

<?php

namespace App\Models\Storage;

use Illuminate\Database\Eloquent\Model;

class Imageup extends Model
{

/**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'image';

protected $fillable = ['id','url'];

public function servlinks() 
{
    return $this->hasMany('App\Models\Services\Link');
}

}

When i want to save i have this error:

Indirect modification of overloaded property App\Models\Services\Link::$linkImage has no effect

I have an idea of what is wrong with my code ?

Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire