-1

I have a model hotel.php to insert hotel data.insert data using create() but it dosen't return id, the returning collection hasn't id field!

Controller.php

/** "/user/2/create" */        
public function store(User $user, HotelRequest $request) 
        { 

            $slug = (new hotel)->uniqueSlug( $request->name );

            $request->merge([ 
                    'cat_id' => 1,
                    'slug' => $slug,
                    'created_by' => auth()->user()->id,
                ]); 

            $hotel = $user->hotels()->create( $request->all() ); 
            dd($hotel);
    ................

hotel.php (model)

    namespace App;
    use Illuminate\Http\UploadedFile;
    use Illuminate\Database\Eloquent\Model;

    class hotel extends Model
    {
        protected $fillable = ['name', 'description','address','street','city','email',
'phone','web','cat_id','slug','created_by'];

        protected  $primaryKey = 'slug'; 

        /** 
            unique slugs genarating
        */
        protected $slug_guards = ['create','room'];

        public $incrementing = false;

User.php (model)

public function hotels( )
{
    return $this->hasmany('App\Hotel');
}

and the final result enter image description here

It dosen't have id attribute. I need id to upload image!

NB: I changed that primarykey to default 'id' but no change in result. enter image description here

Create_hotels... migration

public function up() 
{
    Schema::create('hotels', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned()->index();
        $table->integer('created_by');
        $table->integer('cat_id');
        $table->string('name');
VishnuKumar Pv
  • 167
  • 1
  • 13

5 Answers5

1

Your hotel model set the primary key as the slug :

    protected  $primaryKey = 'slug'; 

What i usually do, is for all my migrations, i set an autoincrements like following :

$table->increments('id');

this way laravel handles everything for you. Each create, update or whatever method handle the id of your items.

With this, you can then return the id of a stored data this way :

$id = create($data)->id;

where $data is your model with new datas. $id should now contain the id value of the newest stored model datas.

kevinniel
  • 1,088
  • 1
  • 6
  • 14
0

You have to change the Hotel model (or remove the whole line):

public $incrementing = true;
Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109
0

You are dumping the data you just inserted into the db which does not contain a id field.

create a show method in your Controller class as follows

public function show(User $user){
    dd($user)
} 

with a route as follows:

Route::get('user/{user}', 'Controller@show')
Marvin
  • 41
  • 1
  • 5
  • Ah... noo when I'm trying this methods in other controller its working... i dumped after creating. So that should be contain an id field.. the other models do. – VishnuKumar Pv Mar 13 '18 at 01:04
0

In hotel.php before the protected $fillable declaration try adding

protected $guarded = [
    'id',
];

In your controllers store() method just after $slug = (new hotel)->uniqueSlug( $request->name );

try adding a $slug->save(); then returned slug should have an ID returned with it.

and/or possible the same similar strategy after line

$hotel = $user->hotels()->create( $request->all() );

by adding $hotel->save();

jeremykenedy
  • 4,150
  • 1
  • 17
  • 25
0

I changed the primarykey to default id and $incrementing = true; now its returning id of the created data. I changed whole methodes in Controller

VishnuKumar Pv
  • 167
  • 1
  • 13