دورة LARAVEL للمبتدئين الدرس الرابع عشر
فهاد الدرس الرابع عشر من دورة laravel للمبتدئين غادي نكملوا الدورة ديالنا وغادي نزيدو la relation ما بين ل user و les posts منبعد غادي نشوفوا كيفاش نديرو نعرضوا اسم المستخدم لي زاد كل post.
1- العلاقة hasMany ف LARAVEL
//<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function posts(){
return $this->hasMany(Post::class);
}
}
2- عرض اسم المستخدم لي زاد كل post
//
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
</head>
<body>
<div class="container">
<div class="row mt-4">
<div class="col-md-6">
<h3>Ajouter un article</h3>
<hr>
{{-- part 4 --}}
@if(Session::has('success'))
<div class="alert alert-success">{{Session::get('success')}}</div>
@endif
{{-- part 3 --}}
@foreach($errors->all() as $error)
<div class="alert alert-danger">{{$error}}</div>
@endforeach
{{-- part 1 --}}
<form action="{{route('post.add')}}" method="post">
{{csrf_field()}}
<div class="form-group">
<input type="text" placeholder="Titre" name="title" class="form-control">
</div>
<div class="form-group">
<textarea placeholder="Description" col="30" rows="5" name="body" class="form-control"></textarea>
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">Valider</button>
</div>
</form>
</div>
<div class="col-md-6">
<h3>Articles</h3>
<hr>
<table class="table table-hover">
<thead>
<tr>
<th>Auteur</th>
<th>Titre</th>
<th>Déscrption</th>
</tr>
</thead>
<tbody>
@foreach($posts as $post)
<tr>
<td>{{$post->user->name}}</td>
<td>{{$post->title}}</td>
<td>{{$post->body}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
3- عرض les posts الخاصين بمستخدم محدد
//
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
</head>
<body>
<div class="container">
<div class="row mt-4">
<div class="col-md-6">
<h3>Ajouter un article</h3>
<hr>
{{-- part 4 --}}
@if(Session::has('success'))
<div class="alert alert-success">{{Session::get('success')}}</div>
@endif
{{-- part 3 --}}
@foreach($errors->all() as $error)
<div class="alert alert-danger">{{$error}}</div>
@endforeach
{{-- part 1 --}}
<form action="{{route('post.add')}}" method="post">
{{csrf_field()}}
<div class="form-group">
<input type="text" placeholder="Titre" name="title" class="form-control">
</div>
<div class="form-group">
<textarea placeholder="Description" col="30" rows="5" name="body" class="form-control"></textarea>
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">Valider</button>
</div>
</form>
</div>
<div class="col-md-6">
<h3>Articles</h3>
<hr>
<table class="table table-hover">
<thead>
<tr>
<th>Auteur</th>
<th>Titre</th>
<th>Déscrption</th>
</tr>
</thead>
<tbody>
@foreach(request()->user()->posts as $post)
<tr>
<td>{{$post->user->name}}</td>
<td>{{$post->title}}</td>
<td>{{$post->body}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>