Blog ب laravel & vue js الجزء الثاني


فهاد الجزء الثاني من blog ب laravel & vuejs غادي نكملوا ل models ديالنا وندوزوا ل controllers لي غادي يكونوا فيهم les fonctions لي غادي نحتاجوا باش نتعاملوا مع قاعدة البيانات ديالنا.

نظرة سريعة بالفيديو

1- تعديل ل MODEL USER

 منبعد غادي نديرو تعديل على ل model User غادي نزيدو la relation مع les posts و les commentaires.

الكود ديال ل User.php بعد التعديل هو : 

                                //
<?php

namespace App;

use App\Post;
use App\Comment;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

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',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function posts()
    {
        return $this->hasMany(Post::class);
    }
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}
                            

2- إضافة ل controller CategoryController

منبعد غادي تزيد controller جديد سميه CategoryController وديرو resource فيه غادي تكون ل fonction index لي كترجعنا les catégories على شكل json وكنحدد فقط les catégories لي عندهم des posts.

الكود ديال CategoryController.php هو :

                                //
<?php

namespace App\Http\Controllers;

use App\Category;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return response()->json(['categories' => Category::has('posts')->get()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function show(Category $category)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function edit(Category $category)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Category $category)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function destroy(Category $category)
    {
        //
    }
}
                            

3- إضافة ل controller PostController

منبعد غادي تزيد controller جديد سميه PostController وديرو resource فيه غادي تكون ل fonction index لي كترجعنا les posts على شكل json وكنحدد فقط 8 ديال les posts.

منبعد عندي ل fonction show لي كترجعنا post بحثنا عليه على شكل json وكترجع معاه حتى les commentaires ديالو بإستعمال ل fonction commentsArray لي كتمكني من إسترجاع les commentaires على شكل json.

منبعد عندي ل fonction category لي كترجعنا les posts الخاصين ب catégorie اختاريناها.

الكود ديال PostController.php هو :

                                //
<?php

namespace App\Http\Controllers;

use App\Post;
use App\Category;
use Illuminate\Support\Carbon;

use Illuminate\Http\Request;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        Carbon::setlocale('fr');
        $posts = Post::latest()->paginate(8);
        foreach($posts as $post){
            $post->setAttribute('user',$post->user);
            $post->setAttribute('added', Carbon::parse($post->created_at)->diffForHumans());
            $post->setAttribute('path', '/post/'.$post->slug);
        }
        return response()->json($posts);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function show(Post $post)
    {
        //
        Carbon::setlocale('fr');
        return response()->json([
            'id' => $post->id,
            'title' => $post->title,
            'photo' => $post->photo,
            'body' => $post->body,
            'created_at' => $post->created_at->diffForHumans(),
            'user' => $post->user->name,
            'category' => $post->category->name,
            'comments_count' => $post->comments->count(),
            'comments' => $this->commentsArray($post->comments)
        ]);
    }

    public function commentsArray($comments){
        $jsonComments = [];
        foreach($comments as $comment){
            array_push($jsonComments,[
                'id' => $comment->id,
                'body' => $comment->body,
                'created_at' => $comment->created_at->diffForHumans(),
                'user' => $comment->user->name
            ]);
        }
        return $jsonComments;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function edit(Post $post)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Post $post)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function destroy(Post $post)
    {
        //
    }

    public function category($slug){
        Carbon::setlocale('fr');
        $categorie = Category::where('slug',$slug)->first();
        $posts = Post::where('category_id',$categorie->id)->get();
        foreach ($posts as $post) {
            $post->setAttribute('user', $post->user);
            $post->setAttribute('added', Carbon::parse($post->created_at)->diffForHumans());
            $post->setAttribute('path', '/post/' . $post->slug);
        }
        return response()->json($posts);
    }
}
                            

4- إضافة ل controller CommentController

منبعد غادي تزيد controller جديد سميه CommentController وديرو resource فيه غادي تكون ل fonction store لي كتزيد commentaire وكترجعونا على شكل json.

الكود ديال CommentController.php هو :

                                    //
<?php

namespace App\Http\Controllers;

use App\Comment;
use App\User;
use Illuminate\Support\Carbon;
use Illuminate\Http\Request;

class CommentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        Carbon::setlocale('fr');
        $comment = Comment::create([
            'post_id' => $request->post_id,
            'body' => $request->body,
            'user_id' => $request->user_id
        ]);

        $user = User::findOrFail($request->user_id);
        return response()->json([
            'body' => $comment->body,
            'created_at' => $comment->created_at->diffForHumans(),
            'user' => $user->name
        ]);
    }


    /**
     * Display the specified resource.
     *
     * @param  \App\Comment  $comment
     * @return \Illuminate\Http\Response
     */
    public function show(Comment $comment)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Comment  $comment
     * @return \Illuminate\Http\Response
     */
    public function edit(Comment $comment)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Comment  $comment
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Comment $comment)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Comment  $comment
     * @return \Illuminate\Http\Response
     */
    public function destroy(Comment $comment)
    {
        //
    }
}
                                

5- إضافة ل controller UsersController

منبعد غادي تزيد controller جديد سميه UsersController  فيه غادي تكون ل fonction register لي كتزيد مستخدم جديد وكترجعونا على شكل json.

عندي أيضا ل fonction login لي كتمكن باش ن connecter المستخدم وكترجعونا على شكل json.

الكود ديال CommentController.php هو :

                                    //
<?php

namespace App\Http\Controllers;
use App\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;

class UsersController extends Controller
{
    //
    public function register(Request $request){
        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        return response()->json([
            'logged' => true,
            'name' => $user->name,
            'email' => $user->email,
            'id' => $user->id,
        ]);
    }

    public function login(Request $request){
        if(auth()->attempt(
            ['email' => $request->email,'password' => $request->password]
        )){
            return response()->json([
                'logged' => true,
                'name' => auth()->user()->name,
                'email' => auth()->user()->email,
                'id' => auth()->user()->id,
            ]);
        }else{
            return response()->json([
                'error' => 'Email ou mot de passe est incorrect'
            ]);
        }
    }
}
                                


إشترك في قناتنا على اليوتيوب

بحث في الموقع


إشترك للتوصل بالجديد