I'm trying to save in my DB the relation between two tables actividads
and fichas
(through a pivot table: actividad-ficha
. And I'm having this message:
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Call to a member function actividads() on a non-object
I pass through my Route two variables (ids):
Route::get('ficha/{id_ficha}/{id_actividad}', array('uses' => 'FichaController@enFicha'));
And then, in my 'FichaController':
public function enFicha($id_ficha, $id_actividad)
{
$ficha = Ficha::find($id_ficha);
// $actividad_id = Actividad::find($id_actividad); (doesnt work)
$actividad_id = $id_actividad;
$ficha->actividads()->sync($actividad_id, false);
return Redirect::route('actividads.index');
}
Here my models:
class Actividad extends Eloquent {
protected $guarded = array();
public function fichas()
{
return $this->belongsToMany('Ficha', 'actividad_ficha')->withTimestamps(); //
}
class Ficha extends Eloquent {
protected $guarded = array();
public function actividads()
{
return $this->belongsToMany('Actividad', 'actividad_ficha')->withTimestamps();
}
After change what @decco and @Ben suggested me, I have the next error message:
Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::formatSyncList() must be of the type array, string given, called in /Applications/XAMPP/xamppfiles/htdocs/webs/lara4/edu1/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php on line 578 and defined
I don't know what is wrong with that... Any idea??
Thank you very much!!
$ficha = $id_ficha;
$ficha->actividads()->sync($actividad_id);
There's your problem. $ficha
is a String
, not an object.
Change the first line with $ficha = Ficha::find($id_ficha)
which should return the Ficha
object with ID $ficha_id
or null
.
Also you need to change the second line with $ficha->actividads()->attach($actividad_id)
.