Having trouble with Laravel Breadcrumbs - laravel

I'm using davejamesmiller's Breadcrumbs package and I have some trouble with getting post category in post view. I want my breadcrumbs to be like this :
Home > Category > Post
I defined breadcrumbs like this:
// Home
Breadcrumbs::for('home', function ($trail) {
$trail->push('Home', route('home'));
});
// Home > [Category]
Breadcrumbs::for('category', function ($trail, $category) {
$trail->parent('home');
$trail->push($category->title, route('category', $category->slug));
});
// Home > [Category] > [Post]
Breadcrumbs::for('post', function ($trail, $post) {
$trail->parent('category', $post->category);
$trail->push($post->title, route('post', $post->slug));
});
And called this code in the post view template to output breadcrumbs:
{{ Breadcrumbs::render( 'course', $course, $category ) }}
But getting this error:
Undefined property: stdClass::$category
And this refers to this line of defined breadcrumb:
$trail->parent('category', $post->category);
My posts table does'nt have category column and I don't know what should I pass as the second parameter of parent method! Should I pass post's category row which is in other table to my post view? What should I pass as second parameter?

I solved my problem by passing $category variable to the breadcrumb that I was defined. like this:
Breadcrumbs::for('post', function ($trail, $post, $category) {
$trail->parent('category', $category);
$trail->push($post->title, route('post', $post->slug));
});
then in my view I changed the output code to:
{{ Breadcrumbs::render( 'post', $post, $category ) }}
I also passed the post's category table row to view from controller.

Related

how to pass one data view and others view page in laravel

i have two pages file in my view
first is my home blade then second is category blade and both of them using extends asidenav
my problem is when im redirecting to category file it gives me a error Undefined variable: categories but in my home file when im redirecting it's working fine . i will describe my code.
in my controller HomeController i pass one data categories to home.blade.php
class HomeController extends Controller
{
public function home()
{
$categories = Category::select('id','name')->get();
return view('home', compact('categories'));
}
public function category(){
return view('category');
}
}
the output of my home.blade.php
#extends('asidenav')
#section('section')
<main>
this is homepage
</main>
#endsection
you can see in my home blade there is a extends asidenav . inside of my asidenav extends the data categories i passed is inside of that asidenav to make a list of categories
the output of my extends asidenav
where the data i pass categories
<ul>
#foreach($categories as $category)
<li><span>{{ $category->name }}
#endforeach
</ul>
when i click the ahref to redirect the category blade it gives the error undefined variable categories. but when im redirecting to home blade it's working
the output of my category.blade.php
#extends('asidenav')
#section('section')
<main>
this is category
</main>
#endsection
but when i tried this code in my category controller it's working fine the error of undefined variable categories not showing
public function category()
{
$categories = Category::select('id','name')->get();
return view('category', compact('categories'));
}
my point here is i want to pass only single data categories in my home blade and others file blade like a props but i don't have a idea how to do that
You could create a service provider responsible for inyecting those variables without declaring them in your controller, in your boot method :
use Illuminate\Support\Facades\View;
View::composer(['asidenav', 'home'], function($view){
$categories = Category::all();
return $view->with('categories', $categories);
})

view not found in Passing ID on create function on laravel

I would like to pass the user_id on create page.
My controller
public function create($user)
{
$loanAmount=LoanAmount::all()->pluck('amount','id');
$userId=User::where('id',$user)->pluck('id')->first();
// $user->id;
// dd($user);
return view('loan/grant-loan-amounts/create/'.$userId)
->with($userId)
->with($loanAmount);
}
Here's my route
Route::resource('loan/grant-loan-amounts', 'Loan\\GrantLoanAmountsController',[
'except' => ['create']
]);
Route::get('loan/grant-loan-amounts/create/{userId}', 'Loan\\GrantLoanAmountsController#create')->name('grant-loan-amounts.create');
I made the create page blank. with only "asd" to display.
what I want to achieve is that, from user list which is on user folder, I'll pass the user id on grant loan amount create a page. But I can't figure out why the route can't be found.
any suggestion?
Don't pass the id in the first parameter of view() function. Only mention the view file in the first parameter of the view function. Pass all variables with compact() function as the second parameter of view(). Example:
public function create($user)
{
$loanAmount=LoanAmount::all()->pluck('amount','id');
$userId=User::query()->findOrFail($user)->id;
return view('user.create',compact('userId','loanAmount')) //in this example 'user' is the folder and 'create' is the file inside user folder
}
Because you have this route:
Route::get('loan/grant-loan-amounts/create/{userId}', 'Loan\\GrantLoanAmountsController#create')->name('grant-loan-amounts.create');
You named the route so you should call it by it's name:
return redirect()->route('grant-loan-amounts.create', $userId);
pluck required one more parameter for fetch data.
public function create($user)
{
$loanAmount=LoanAmount::all()->pluck('amount','id');
$userId=User::findOrFail($user);
return view('YourBladeFileFolderLocationOrFileLocationHere')->compact('userId','loanAmount');
}
now you can fetch or write at blade file like
For Id :
{{ $userId->id }}
For User Name :
{{ $userId->name }}

why I get error on current route name with id

in my app I use link as language switcher, it works ok in all web routing and show the correct button for language switch but, in my product page with id I get this error:
Missing required parameters for [Route: products] [URI: {lang}/products/{id}]
this is the app web route:
Route::group(['prefix' => '{lang}'], function () {
Route::get('products/{id}', 'AppController#products')->name('products');
});
this is the controller:
public function products($lang, $id){
$products = Category::with('products')->where('id', $id)->get();
return view('products', compact('products', 'lang'));}
and this is the buttons I use for language switch:
#if(app()->isLocale('fa'))
<div id="change">English</div>
#elseif(app()->isLocale('en'))
<div id="change">Farsi</div>
#endif
AS I said the language switch work ok in all routes except in product with :id
The products route you've defined requires an ID and you are not putting one in the route generator.
Your code is a bit confusing, so what I think you are doing is this:
You are displaying a list of products in a category of $id, with one single link to switch languages. You will need to update your route to include a the single common product id:
public function products($lang, $id){
$products = Category::with('products')->where('id', $id)->get();
$product_id = $id;
return view('products', compact('products', 'product_id', 'lang'));
}
Then the output:
#if(app()->isLocale('fa'))
<div id="change">English</div>
#elseif(app()->isLocale('en'))
<div id="change">Farsi</div>
#endif
Should work for you.
You're missing a parameter in your products route. Only parameter you defined is id, but your controller expects lang as well, which you don't pass. Change your route definition to:
Route::get('products/{lang}/{id}', 'AppController#products')->name('products');
Also, remove curly brackets from your route prefix, since this is only the name of the route, and shouldn't be defined as a parameter:
Route::group(['prefix' => 'lang'], function ()

Problems with undefined variable in Laravel

I keep getting Undefined variable: cars when loading the page in Laravel.
I have the following code:
web.php
Route::get('/cars', function () {
$cars= Car::select('brand')->get();
return view('test.cars')->with(compact($cars));
});
If I put dd(cars); before return view the collection is outputted.
In cars.blade.php I have the following code:
<select name="car">
#foreach($cars as $car)
<option>{{ $car->brand}}</option>
#endforeach
</select>
and I get Undefined variable: cars, I also removed the dropdown from blade template and I tried {{ dd($cars) }} but get the same error.
Try the below code :
Route::get('/cars', function () {
$cars= App\Car::pluck('brand');
return view('test.cars',['cars'=>$cars]);
});
OR
Route::get('/cars', function () {
$cars= App\student::pluck('first_name');
return view('test.cars', compact('cars'));
});
Note: Car model is present in App folder here.
The correct syntax for passing variables to views is either:
view('test.cars', ['cars' => $cars])
or
view('test.cars')->with('cars', $cars)
the "with" function requires a name to be passed first, followed by the variable.

Laravel - How to return last value from user input into a view

I'm building a backend for my website in Laravel. I need only few text boxes to populate a template. So far I've managed to do that, but every time I pass a value to a template I see also all previous values, not only the last one.
So for example if I'm editing slider title, I see also all previous slider titles - I want to see only last one.
My controler:
public function store()
{
$input = Input::all();
homepage::create([
'slider_title' => $input['slider_title'],
]);
return Redirect::to('/');
}
Part of view responsible for displaying slider title:
#foreach ($homepage as $home)
{{$home->slider_title}}
#endforeach
View for user input in admin:
{{ Form::open(['url' => '/admin']) }}
<div>
{{ Form::label('slider_title', 'Slider title:') }}
{{ Form::text('slider_title') }}
</div>
<div>
{{ Form::submit('Change a slider title') }}
</div>
{{ Form::close() }}
Route for admin:
Route::post('/admin', 'AdminController#store');
Route for main page:
Route::get('/', 'PageController#home');
PageController home method:
public function home()
{
$homepage = Homepage::all();
return View::make('home')->with('homepage',$homepage);
}
I tried changing PageControl with:
$homepage = Homepage::orderBy('id', 'desc')->first();
but I'm getting this error:
ErrorException: Trying to get property of non-object on my view page
public function home()
{
$homepage = Homepage::all();
return View::make('home')->with('homepage',$homepage);
}
You are seeing all slides because you are selecting all of them:
$homepage = Homepage::all();
This should give you the last one:
$homepage = Homepage::orderBy('id', 'desc')->first();
or
$homepage = Homepage::orderBy('created_at', 'desc')->first();
Remember that now you are getting only one result and you cannot use foreach anymore:
foreach ($homepage as $home)
You can refer to it directly:
$homepage->slider_title
If you still want to return an array to use foreach, this is an option:
$homepage = array( Homepage::orderBy('created_at', 'desc')->first() );

Resources