User id, name, email
Address id, user_id, address
Phone id, user_id, mobile
User To [Address, phone]
User Model public function getAddress(){ return $this->hasOne('App\Address'); return $this->hasOne(Address::class); return $this->hasOne(Address::class, 'foreign_key'); return $this->hasOne(Address::class, 'user_id'); return $this->hasOne(Address::class, 'foreign_key', 'local_key/Primary_key'); return $this->hasOne(Address::class, 'user_id', 'id'); } public function getPhone(){ return $this->hasOne(Post::class); }
use App\User; use App\Post; public function index(){ $data['Users'] = User::all(); $data['Users'] = User::with('getAddress', 'getPhone')->get(); foreach ($data['Users'] as $User) { echo "Name: " .$User->name. "<br>"; echo "Address: " .$User->getAddress->address."<br>"; echo "Phone" .$User->getPhone->phone; } }
@php $users = App\User::all(); @endphp @foreach($users as $user) {{ $user->name}} {{ $user->getAddress->address}} {{ $user->getPhone->Phone}} @endforeach
Phone id, user_id, phone
Phone To [User]
public function user(){ return $this->belongsTo(User::class); } N:B: In this "belongsTo" function Name always will be model name. Example: If model name "User", the function name will be "user or User". But if use "getUser" , the function will be face problem. So always function name will be as like "Model" name.
use App\User; use App\Phone; public function index(){ $phones = Phone::all(); foreach ($phones as $phone) { echo "Phone: " .$phone->phone. "<br>"; echo "Name: " .$phone->user->name."<br>"; } }
@php $phones = App\Phone::with('user')->get(); @endphp @foreach($phones as $phone) {{ $phone->phone}} {{ $phone->user->name}} {{ $phone->user->email}} @endforeach
Post id, post
Comment id, post_id, comment
Post To [Comment]
public function comments(){ return $this->hasMany(Comment::class); }
use App\Post; use App\Comment; public function index(){ $posts = Post::all(); foreach ($posts as $post) { foreach ($post->comments as $comment) { echo $comment->comment; } } }
@foreach($posts as $post) <tr class="text-center"> @php $rowspan =$post->comments->count(); $totalRow = $rowspan+1; @endphp <td rowspan="{{$totalRow}}">{{ $post->id}}</td> <td rowspan="{{$totalRow}}">{{ $post->post}}</td> @foreach($post->comments as $comment) <tr> <td>{{ $comment->comment}}</td> </tr> @endforeach </tr> @endforeach
User id, name
Category id, categoryName
Post id, user_id, category_id, post, description
Post To [User, Category]
public function user(){ return $this->belongsTo(User::class); } public function category(){ return $this->belongsTo(Category::class); }
use App\User; use App\Category; use App\Post; public function index(){ $posts = Post::all(); foreach ($posts as $post) { echo $post->id; echo $post->user->name; echo $post->category->categoryName; echo $post->post; echo $post->description; } }
@php $posts = App\Post::all(); @endphp @foreach($posts as $post) <tr class="text-center"> <td>{{ $post->id}}</td> <td>{{ $post->user->name}}</td> <td>{{ $post->category->categoryName}}</td> <td>{{ $post->title}}</td> <td>{{ $post->description}}</td> </tr> @endforeach
Post id, user_id, category_id
User to [Category]
public function category(){ return $this->belongsToMany('App\Category', 'App\Post'); // return $this->belongsToMany(Category::class, Post::class); // return $this->belongsToMany('App\Category', 'App\Post', 'user_id', 'category_id'); }
use App\User; use App\Category; use App\Post; $users = App\User::all(); foreach($users as $user){ echo $user->name; foreach($user->category as $categoryName) echo $categoryName->categoryName; } }
@php $users = App\User::all(); @endphp @foreach($users as $user) <tr class="text-center"> @php $rowspan =$user->category->count(); $totalRow = $rowspan+1; @endphp <td rowspan="{{$totalRow}}">{{ $user->id}}</td> <td rowspan="{{$totalRow}}">{{ $user->name}}</td> @foreach($user->category as $categoryName) <tr> <td>{{ $categoryName->categoryName }}</td> </tr> @endforeach </tr> @endforeach