Attempt to read property "title" on null - Laravel - laravel

when I enter the detail page in Laravel, it gives an error. I don't understand where the error is coming from. My codes are as follows.
where is the problem? actually it says it's empty, but it needs to pull data from $blog on controller side.
Controller:
public function show($id)
{
$blog = Blog::where('id',$id)->first();
return view('front.detail',compact('blog'));
}
routes/web.php:
Route::prefix('{lang?}')->middleware('locale')->group(function() {
Route::get('/', [MainController::class, 'index'])->name('home');
Route::get('/about', [MainController::class, 'about'])->name('about');
Route::resource('/blogs', MainController::class)->only([ 'show']);
});
detail.blade.php:
<li>
<h2>{{$blog->title}}</h2>
<p>{!! $blog->text !!}</p>
</li>

If you want to get a model by the main column you use find not where and the column:
public function show($id)
{
$blog = Blog::find($id);
return view('front.detail',compact('blog'));
}
Then, find can return the Model (hence an object) or null, so in your case it is not finding the model by ID.
What you should use is findOrFail so it throws an exception if it did not find the model:
public function show($id)
{
$blog = Blog::findOrFail($id);
return view('front.detail',compact('blog'));
}
Then you can continue debugging why it is not found

Doesn't appear you are passing the blog ID in with your URL:
Route::resource('/blogs', MainController::class)->only([ 'show']);
Try changing your route to this:
Route::resource('/blogs/{id}', MainController::class)->only([ 'show']);
Your URL should be something like (where 5 is the blog ID):
http://yourdomain.com/blogs/5
You could also use binding:
// Route
Route::get('/blogs/{blog}', [MainController::class, 'show']);
public function show(Blog $blog)
{
return view('front.detail', ['blog' => $blog]);
}
More info can be found here:
Route model binding

Related

Laravel 8 Find Method result 404 Not Found

I just start learning Laravel 8, In my learning project I am using find($id) Method but it ended with 404 Not Found. In my database id is the primary key.
Here are the code I wrote
<td>
Detail
Update
Delete
</td>
For Controller :
public function show($id)
{
$detail = Member::find($id);
return response()->json($detail);
}
Route :
Route::get('/detail/id', [MemberController::class, 'show']);
Please help me, what is wrong with my code?
You are missing curly brackets {id} in the route definition
Route::get('/detail/{id}', [MemberController::class, 'show']);
public function show($id)
{
return Member::findOrFail($id);
}
https://laravel.com/docs/8.x/routing#route-parameters
You can also use route model binding
In your case it will be
Route::get('/detail/{member}', [MemberController::class, 'show']);
public function show(Member $member)
{
return $member;
}
https://laravel.com/docs/8.x/routing#route-model-binding

Laravel: undefined variable posts within website

Really struggling here, trying to display the posts on my page, bare in mind these posts are displaying on other pages. However i cannot display them on my index.php page.
welcome.blade.php
#if(count($posts) > 1)
#foreach($posts as $post)
<h2>{{$post->title}}</h2>
#endforeach
#else
</p>no posts found</p>
#endif
WelcomeController.php
public function index()
{
$posts = Post::all();
return view('Pages.welcome')->with('posts', $posts);
}
PostsController.php
public function index()
{
$posts = Post::all();
return view('posts.index')->with('posts', $posts);
}
Web.php (routes)
Route::get('/', 'PageController#index');
Route::get('/welcome','PageController#Welcome');
Route::get('/services', 'PageController#services');
Route::get('/register', 'PageController#register');
Route::get('/Create', 'PageController#Create');
Route::get('/search', 'PageController#search');
Route::get('/payment', 'PageController#Payment');
Route::resource('posts', 'PostsController');
Route::resource('search', 'SearchController');
Route::resource('reviews', 'ReviewsController');
HomeController.php
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
I have put postscontroller because this controller works and displays data to other pages (posts.index), however when i try to display data on Pages.index i am unable to do so ?
Make sure you are accessing the right path on your URL. In this case you should access localhost:8000/welcome .
In your route
Route::get('/index','PageController#HomePage');
Shoud be
Route::get('/welcome','WelcomeController#index');
because your controller file name is WelcomeController.php and you need to redirect to your index function in that controller.
Make sure you are redirecting to the right view in your controller
public function index()
{
$posts = Post::all();
return view('Pages.welcome', compact('posts'));
}
Here you are seeking the welcome.blade.php file in your /Pages directory
Your controllers and routes are fine but you miss-spell foreach
#foreac h($posts as $post)
Replace with :
#foreach($posts as $post)
now this should work

how to send id in route and controller in laravel localization

In my Laravel project with localization I made middleware, route group and all parameters, language switch work correct but when I click to send id by
I get the error:
Missing required parameters for [Route: products] [URI:
{lang}/products/{id}]
My Routes:
Route::group(['prefix' => '{lang}'], function () {
Route::get('/', 'AppController#index')->name('home');
Route::get('/categories', 'AppController#categories')->name('categories');
Route::get('products/{id}', 'AppController#products')->name('products');
Auth::routes();
});
My Middleware:
public function handle($request, Closure $next)
{
\App::setLocale($request->lang);
return $next($request);
}
My AppController:
public function products($id)
{
$products = Category::with('products')->where('id', $id)->get();
return view('products', compact('products'));
}
this is the URL:
http://127.0.0.1:8000/fa/products/1
if I change the above URL manually it works and shows the page:
http://127.0.0.1:8000/1/products/1
But if I click on:
I receive the error.
Since you added a route prefix the first parameter of the products method in your controller will be lang and the second one id.
This should fix the controller:
public function products($lang, $id)
{
$products = Category::with('products')->where('id', $id)->get();
return view('products', compact('products', 'lang'));
}
You need to use a key-value array in route('products', ['lang'=>app()->getLocale(), 'id'=>$category->id]) or whatever your route parameters are named in the original route.
Ref. Laravel Named Routes
PS. as Remul notes, since you have a lang param (as route prefix) the first param in your controller will be $lang then $id
public function products($lang, $id)
{
$products = Category::with('products')->where('id', $id)->get();
return view('products', compact('products'));
}

Getting error in Laravel 5.7 edit route page not found

Laravel Version 5.7
PHP 7+
I created a resource controller -> CategoryController [having all the magic methods]
This is the routes/web.php
Route::group(['as'=>'admin.','middleware'=>['auth','admin'],'prefix'=>'admin'], function(){
Route::get('/dashboard','AdminController#dashboard')->name('dashboard');
// product resource controller methods
// check php artisan r:l
Route::resource('product', 'ProductController');
Route::resource('category', 'CategoryController');
Route::resource('profile', 'ProfileController');
Route::post('remove', 'CategoryController#remove')->name('category.remove');
});
Now as you can see, I have "http://127.0.0.1:8000/admin/category/1/edit" for one of my categories to edit with category id = 1, that is also stored in the database.
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
public function index()
{
$categories = Category::paginate(3);
return view('admin.categories.index',compact('categories'));
}
public function edit(Category $category)
{
return "This is category edit page";
// dd($category);
// $categories = Category::where('id','!=', $category->id)->get();
// // dd($categories);
// return "This is category edit page";
// return view('admin.categories.create',['categories' => $categories, 'category'=>$category]);
}
When I try to go to this edit category page, it shows 404 page not found error.
Although, when I made an individual route for edit method with a closure function to return some text, it worked perfectly.
Route::get('category/{category}/edit', function($category){
return $category;
})->name('category.edit');
You didn't excluded full error you get, but try to change:
public function edit(Category $category)
{
return "This is category edit page";
}
into:
public function edit($category)
{
return "This is category edit page";
}
and see if it helps. If it helps, it means that there is no record matching id you passed or this record is soft deleted (or some additional conditions are not met) - Laravel uses Route model binding to match valid record.
try this
public function edit(Request $category)
{
return "This is category edit page";
}

Laravel 5 - How to passing request data from route to controller

I am new in laravel,
How to passed request data into controller ? Just like happen on view ?
Route::get('/kelihatan', function (Request $request) {
return view( 'pages' , [ 'page' => 'index' , '_request' => $request->all() ] );
});
How to passing request data to controller before passed into view or model ? Just like this ??
Route::get( '/{page}', 'UserController#show' );
Mention the path in route first depending upon the type of request i.e. Get or Post
Route::get('/invite/{code}', 'MyController#get_my_action');
Route::post('/invite/{code}', 'MyController#post_my_action');
Then in controller named as MyController create a function like
public function get_my_action($code){
//your code goes here
}
or
public function post_my_action(Request $request, $code)
{
//your code goes here
}
With that route:
Route::get('/{page}', 'UserController#show');
In the controller you can do that:
public function show()
{
$page = request()->route('page');
or that:
public function show($page)
or that:
public function show(Request $request, $page)
Try this:
Routes.php
Route::get( '/{page}', 'UserController#show' );
UserController.php
public function show($page) {
dd($page);
}
Inside your controller define your show function as below:
function show(Request $request)
{
echo $request['page'];//or whatever data you want to pass
}
///add follwoing line before you define controller
use Illuminate\Http\Request;

Resources