I'm new to Laravel 5.0.
I'm trying to make a hasmany relationship between my two models, User and Device.
If in tinker I do App\user::first()->device the result is null
I populated the users and devices tables with some dummy data and the single find() work.
I searched on StackOverflow but I could't find out a solution
Here's my users table
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
my devices table
public function up()
{
Schema::create('devices', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->double('lat', 15, 8);
$table->double('lon', 15, 8);
$table->timestamps();
});
}
and my function in the User Model
public function device()
{
return $this->hasMany('App\Device');
}
Thank you all
The code was right, I found out sometimes tinker needs to be rebooted after the changes in the code
Related
I'm trying to pass the foreign key but am stuck with this error Base table or view already exists: 1050 Table 'favorites' already exists"
how can I pass them all the foreign keys and the normal id like $table->bigInteger('user_id')->unsigned();
tables
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->Increments('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('product_id')->unsigned();
$table->timestamps();
});
Schema::create('favorites', function (Blueprint $table){
$table->bigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products');
});
}
public function down()
{
Schema::dropIfExists('favorites');
}
You're migrating the table twice, pass the foreign keys in the same migration
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('favorites');
}
Hope this helps
Other answers are correct. If ever you've missed the needed tasks before doing it, kindly try this one
1) Remove or delete the table you've migrated
2) In migrations table, delete the record which consists the name "favorites"
3) Copy and paste thi code inside the "up" function with this one
Schema::create('favorites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('product_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('product_id')->references('id')->on('products');
});
4) Run this command again
php artisan migrate
I'm trying to use a belongsToMany relation, i never had any problems with it but for some reason it returns empty.
I've set up my relation in the Store model like this:
public function images()
{
return $this->belongsToMany('App\Images', 'images_stores', 'image_id', 'store_id');
}
When i test the relationship it returns no error just a empty collection although it's not. This is the way i access the data:
public function edit($id)
{
$store = Store::find($id);
dd($store->images);
return view('admin.pages.store.edit', compact('store'));
}
But it would just return a empty collection, i hope someone could help me out with this. These are my migration file's but i don't think the problem is inside the migration files.
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('path');
$table->timestamps();
});
// pivot table
Schema::create('images_stores', function (Blueprint $table) {
$table->increments('id');
$table->integer('image_id')->unsigned()->nullable();
$table->foreign('image_id')->references('id')->on('images')->onDelete('cascade');
$table->integer('store_id')->unsigned()->nullable();
$table->foreign('store_id')->references('id')->on('stores')->onDelete('cascade');
$table->timestamps();
});
// store table
Schema::create('stores', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('location');
$table->string('email');
$table->timestamps();
});
Thanks guys.
I think the ids are inverted. Try this:
public function images()
{
return $this->belongsToMany('App\Images', 'images_stores', 'store_id', 'image_id');
}
I'm trying to get the users that are on today's shift. I have a many to many relationship between the User and Shift model with a pivot table called user_shifts.
My User model:
/**
* Get the users shifts
*/
public function shift()
{
return $this->belongsToMany('App\Shift', 'user_shifts', 'user_id', 'shift_id');
}
My Shift model
/**
* The shift can have many users
*/
public function users()
{
return $this->belongsToMany('App\User', 'user_shifts', 'shift_id', 'user_id')->withPivot('start_time', 'end_time');;
}
In my controller I thought I could do something like:
$shift = Shift::all()->where('date', date('Y-m-d'));
$users = User::all()->where('shift_id', $shift);
return $users;
But that is returning null. Any help would be appreciated!
If it matters my db schema is:
Schema::create('shifts', function (Blueprint $table) {
$table->increments('id');
$table->date('date');
$table->boolean('weekend');
});
Schema::create('user_shifts', function (Blueprint $table) {
$table->increments('id');
$table->integer('shift_id');
$table->integer('user_id');
$table->time('start_time');
$table->time('end_time');
});
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('fname');
$table->string('lname');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
I currently have a Users to Groups Relationship (ManyToMany) with a pivot table group_user. I want the user to be able to create a group but once creating the group, how do I make it, that the creator becomes member of this group?
Currently I have
My Pivot Table (group_user):
Schema::create('group_user', function(Blueprint $table)
{
$table->integer('group_id')->unsigned()->index();
$table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
My Groups table (groups):
Schema::create('groups', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->timestamps();
});
My Users table (users):
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('name');
$table->string('lastname');
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
My models ofcourse have the following: User.php
public function groups()
{
return $this->belongsToMany('App\Group');
}
Group.php
public function users()
{
return $this->belongsToMany('App\User');
}
What create function should I write in my controller so that when a User creates a Group, that he automaticly becomes member of this group (automaticlly make the pivot relationship)?
This should work, make sure you implement validation, ect.
public function store(Request $request)
{
$group = Group::create([ // <-- if names are unique. if not, then create is fine
'name' => $request->get('name')
]);
auth()->user()->groups()->attach([$group->id]);
return view('your.view');
}
Also make sure to add:
use App\Group;
See attach() and detach().
$user = User::find(1);
$user->groups()->attach(10); // pivot relationship of this user to group of id 1.
OR
$group = Group::find(10);
$user->groups()->save($group);
For many groups of this user:
$user->groups()->sync(array(1, 2, 3));
I am new to Laravel PHP framework. I am confused with how Laravel schema builder and migrations work. I created two tables using Laravel migrations. Below are my two tables:
public function up()
{
Schema::create('hotels', function(Blueprint $table)
{
$table->increments('id');
$table->integer('hotel_id');
$table->string('name');
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('province')->nullable();
$table->string('country')->nullable();
$table->timestamps();
});
}
This is my other table:
public function up()
{
Schema::create('rooms', function(Blueprint $table)
{
$table->increments('id');
$table->integer('hotel_id');
$table->string('name');
$table->timestamps();
});
}
My question is how can can I make relations with two tables using Foreign keys? If I have to use Schema Builder where do I have to put the schema file?
You can do it in the very same migrations, using Schema Builder. This is how you create a foreign key using migrations:
public function up()
{
Schema::create('rooms', function(Blueprint $table)
{
$table->increments('id');
$table->integer('hotel_id');
$table->string('name');
$table->foreign('hotel_id')
->references('id')
->on('hotels')
->onDelete('cascade');
$table->timestamps();
});
}