bigIncrements('for ID');
unsignedBigInteger('user_id');
string('for title, url')->nullable();
text('for description');
timestamps();
$table->index('user_id'); - this one is for adding quicker queries. We should add index for any foreign key in our table. user_id is a foreign key, which is key in a different table.
Now we can migrate it and let Laravel know how to handle this relationship properly.
Let say a user has one profile and profile belongs to one user. This is a one to one relationship.
So, how to do it?
After doing the migration, we can go to Profile.php model from our example of User has Profile.
Here we need to add public function:
public function user()
{
return $this->belongsTo(User::class);
}
Now, at User.php model we have to connect it with Profile by adding as well public function:
public function profile()
{
return $this->hasOne(Profile::class);
}
If you don't have a namespace App; at the top of your file, you would have to add before Profile in your function App\Profile to make it work. This is just basic stuff in PHP and has nothing to do with Laravel.
Now, it's time to work with Tinker, which is working with PHP line after line. So:
php artisan tinker
$profile = new \App\Profile();
$profile->title = 'Cool Title';
$profile->description = 'Description';
If we have something not nullable like let say user_id, we have to add as well in the Tinker:
$profile->user_id = 1;
Now, when everything is done, we can save it:
$profile->save();
Now, to test it, we can call:
$profile->user
Now we're able to fetch user through a profile. So, our relationship works fine.
$profile - to show profile including user.
Now we can do it in a reverse:
$user = App\User::find(1);
This should give user details with id 1.
$user->profile
This should show just a profile.
Now you can go and create View to fetch the data and display it on the screen.
How to get it in the View file?
{{ $user->profile->title }}
where the title is a column in our profile table.
{{ $user->profile->description }}
to get the description out of the profile table. If something is nullable, Laravel with just simply give Us an empty space.
What to do with an empty nullable field?
We can use or, which is ?? and create syntax as:
{{ $user->profile->url ?? 'N/A' }}
This if URL is nullable, and has no data inside, will display Us only N/A. But, this will create a link because we have a url that won't point anywhere.
We can go to tinker and fix it there:
$user->profile->url = 'our_url_here';
To save it in this case, we have to use the command:
$user->push();
-----------------------------------------------------------------------------------------
Now relationship one to many:
User to post where user can have many posts.
1. Create a model post with migration.
2. Edit migration:
bigIncrements('id');
unsignedBigInteger('user_id');
string('caption or short description');
string('image');
timestamps();
$table->index('user_id');
Now it's time to migrate it.
After this, we can go to our User.php model to add another public function:
public function posts()
{
return $this->hasMany(Post::class);
}
Why plural this time? Because the relationship is one to many, so many posts. Now, in our new Post.php model, we have to do the inverse:
public function user()
{
return $this->belongsTo(User::class);
}
Now we are all setup. The next step is to either add stuff by Tinker or create a form to add it.
In order to create via form we can create a new route for create:
Route::get('/p', 'PostsController@create');
Where get is a method, '/p' is a link to go to and postcontroller we will now create to control it with create method. So now it's time to create controller:
PostsController
Once done we can go to it. Inside it's time for the first function:
public function create()
{
return view('posts.create');
}
This function will return page from folder 'posts' which is in the folder 'layouts' in folder 'Views', we should create this folder 'posts' to keep our code tidy. Inside this 'posts' folder, we should create a file called 'create.blade.php'.
No comments:
Post a Comment