rasoul_rezazadeh's avatar

id

hi. i want to create article details page. so i link article title with: href="/articledetails/{{$article->id}}" and recieve that (id) in the router(web.php) by: /articledetails/{article} but article details page doesn't work correctly! css styles not working. but when i delete id that works correctly. what should i do for solve this problem?

0 likes
19 replies
automica's avatar

@rasoul_rezazadeh your view method should look like

public function view(Article $article) {


return view('articles.show',compact('article'))

}

you don't need to put id in your link as if you typehint the model, the view method will know to use your primary key.

 href="/articledetails/{{$article}}" 

as for why your page doesn't display its css correctly, that's a different problem and is more than likely due to css link not starting with a forward slash to make it load css from top level directory.

saurav77's avatar

@rasoul_rezazadeh

public function detailPage($id){
	$article=Article::find($Id);
       return view('articles.detail',compact('article'));
}

//Route

Route::get('/article/{id},''ArticleController@detailPage")->name('detailPage');

//link
<a href="{{route('detailPage','$article->id')">
automica's avatar

@saurav77

your link isn't correct. you got single and double quotes mixed up and some brackets missing.

should be:

//link

<a href="{{ route('detailPage',$article->id) }}">

where route is:

Route::get('/articledetails/{id},'ArticleController@detailPage')->name('detailPage');
rasoul_rezazadeh's avatar

my view not working now! check my codes please: link: href="/articledetails/{{$article}}"

Route::get('/articledetails/{article}',[articlecontroller::class,'articledetails']);

public function articledetails(article $article) { return view('article/blogdetails',compact('article')); }

laracoft's avatar

@rasoul_rezazadeh do try and format your code using ```, without it, it is very hard to read and help.

/neederrorlog When you say view not working, what is the error you are getting?

automica's avatar

@rasoul_rezazadeh you've copied my code wrong.

 articledetails(article $article) { 
return view('article/blogdetails',compact('article'));
 }

should be

 articledetails(Article $article) {
 return view('article/blogdetails',compact('article')); 
}

the model name should be capitalized so 'Article' not 'article', matching the class name in your Article class

automica's avatar

@rasoul_rezazadeh your controller name in your route should match the controller

ArticleController::class

not

articlecontroller::class
automica's avatar

@rasoul_rezazadeh if you still have a 404 then check your route is correct by running

php artisan route:list

and you should see it there.

rasoul_rezazadeh's avatar

not working again! checked route & it was correct.

href="/articledetails/{{$article}}"

Route::get('/articledetails/{article}',[ArticleController::class,'articledetails']);

public function articledetails(Article $article) { return view('article/blogdetails',compact('article')); }

rasoul_rezazadeh's avatar

href="/articledetails/{{$article}}"

Route::get('/articledetails/{article}',[ArticleController::class,'articledetails']);

public function articledetails(Article $article) { return view('article/blogdetails',compact('article')); }

rasoul_rezazadeh's avatar

working!! thnx.

i created details page with yield & sections.you see my details page:


@extends('layout.master')

@section('title')
    Article Details
@endsection

@section('content')
....
@endsection

but css not working.do you know what?

rasoul_rezazadeh's avatar

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>@yield('title')</title>
    <!-- fontawesome css link -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
          integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="assets/css/fontawesome-all.min.css">
    <!-- flaticon css -->
    <link rel="stylesheet" href="assets/font/flaticon.css">
    <!-- magnific popup -->
    <link rel="stylesheet" href="assets/css/magnific-popup.css">
    <!-- nice-select css -->
    <link rel="stylesheet" href="assets/css/nice-select.css">
    <!-- bootstrap css link -->
    <link rel="stylesheet" href="assets/css/bootstrap.min.css">
    <!-- swipper css link -->
    <link rel="stylesheet" href="assets/css/swiper.min.css">
    <!-- favicon -->
    <link rel="shortcut icon" href="assets/images/fav.png" type="image/x-icon">
    <!-- animate.css -->
    <link rel="stylesheet" href="assets/css/animate.css">
    <!-- main style css link -->
    <link rel="stylesheet" href="assets/css/style.css">
    <!-- responsive css link -->
    <link rel="stylesheet" href="assets/css/responsive.css">
</head>

that works for addtitle and another pages. but not working for details page.

Please or to participate in this conversation.