Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 5.7 data not passed to the view

I'm trying to pass my article data to the single page article named article.blade.php although all the data are recorded into the database but when I tried to return them in my view, nothing showed and the [ ] was empty. Nothing returned.

this is my articleController.php

<?php
namespace App\Http\Controllers;

use App\Article;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    public function single(Article $article)
    {
        return $article;
    }
}

this is my model:

<?php

namespace App;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use Sluggable;

    protected $guarded = [];

    protected $casts = [
        'images' => 'array'
    ];

    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }

    public function path()
    {
        return "/articles/$this->slug";
    }

    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }
}

and this is my Route

Route::get('/articles/{articleSlug}' , 'ArticleController@single');
like image 337
Morez Dev Avatar asked Nov 22 '25 12:11

Morez Dev


1 Answers

Change your code to

class ArticleController extends Controller
{
    public function single(Article $article)
    {
        return view('article', compact('article'));
    }
}

change route to

Route::get('/articles/{article}' , 'ArticleController@single');

And model

public function getRouteKeyName()
{
    return 'slug';
}

See docs https://laravel.com/docs/5.7/routing#route-model-binding

like image 114
Davit Zeynalyan Avatar answered Nov 25 '25 00:11

Davit Zeynalyan