mardi 24 avril 2018

Laravel: Too few arguments to function App\Status::create(), 1 passed and exactly 2 expected

I have two models (Categories and Channels) in a Morph Relation with Status (active, inactive).

In the moment of creating a new category or a new Channel I need to assign the active status to the new created element. For that, I need to pass the $type (channel or category) and the id of the "statusable" element.

Here is my CategoryController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\CategoriesRequest;
use App\Profile;
use App\Status;
use Session;
use Auth;

class CategoriesController extends Controller
{
 public function store(CategoriesRequest $request)
    {

        $category = Category::create([
            'title'             => $request->title,
            'slug'              => str_slug($request->title, '-'),
            'about'             => $request->about,      
       ]);   

        $category->save();

        $type =  'categories';
        $id = $category->id;
        $status = (new Status)->create($id, $type);   <-- Hier I am passing the two variables to the function CREATE in Status Model

        Session::flash('success', 'Category successfully created!');
        return redirect()->route('categories.show', $category->slug);
    }
}

And hier the STATUS Model with the CREATE method which receive the two variables

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use App\Status;

Relation::morphMap([

    'channels'      => 'App\Channel',
    'categories'    => 'App\Category',
]);

class Status extends Model
{

   protected $fillable = [

        'status',
        'statusable_type',
        'statusable_id',

    ];

    public function statusable()
    {
        return $this->morphTo();
    }

    public static function create($id, $type)
    {                       // I do hier dd($id); and dd($type) and the two variables have a value
        $status = Status::create([
            'statusable_id'     => $id,
            'statusable_type'   => $type,                        
            'status'            => 'active',    // I get the error here! 
        ]);
        $status->save();
        return $status;
    }
}

I confirmed that the two variables arrived to the CREATE method (I see then if I dd() it) right at the beginning of the method. However two lines after I get this error:

Type error: Too few arguments to function App\Status::create(), 1 passed in C:\laragon\www\streets\app\Status.php on line 41 and exactly 2 expected

What am I doing wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire