Sharing variable to laravel layouts - php

I need to share variable to standart layout.app, but when i try to do it-the variable in blade is undefined.
There is the controller part:
public function SidebarOffice(){
$facultys=Faculty::all();
return view('layouts.app', compact('facultys'));
}
And the layout.app part
#foreach($facultys as $faculty)
<a class="dropdown-item" href="#">{{$faculty->id}}</a>
#endforeach
I also saw a way with a View::share. Can it work out with him?

Trying doing it with 'with' method.
It should look something like this:
return view('layouts.app')->with(compact('facultys')));

return view('layouts.app',['facultys'=>$facultys]);

Related

Call controller with href on Laravel

I'm trying to call controller with href, but I'm getting error, I need pass a parameter.
Im doing like this
<i class="material-icons" title="Delete"></i>
Controller Code
public function destroy(Story $story)
{
$story = Story::find($id);
$story->delete();
return redirect('/stories')->with('success', 'Historic Removed');
}
Error Missing required parameters for Route: stories.destroy -> error
The link_to_action() helper generates an actual HTML link, which is an <a> tag. You're therefore already using it wrong.
However the error you're getting is likely not related to this.
The best way to link to routes is using the route() helper:
link
And the route definition:
Route::get('/someroute/{:param}', ['uses' => 'IndexController#index', 'as' => 'index.index']);
Note the as key, it assigns a name to this route. You can also call
Route::get(...)->name('index.index')
which yields the same result.
I might be wrong but in html you're passing an integer, in controller though, function is expecting an object of Story. Just change Story story to $id and it should be good.
Anyway, can't say much more without actual error.
Since you are accepting $story as model object so you don't have to use Story::find() and also you haven't define $id in your destroy method therefor Change your code to:
public function destroy(Story $story)
{
$story->delete();
return redirect('/stories')->with('success', 'Historic Removed');
}
Hope it helps.
Thanks
You should use it in this way: since according laravel explanation for function link_to_action first param will be controller function path, 2nd will be name and 3rd will be array of required params:
<i class="material-icons" title="Delete"></i>
You can also get help from here

Problem showing the view when I add a parameter in my Route - Laravel

I want to pass a parameter to my controller from an URL, my code is:
In web.php:
Route::get('infoPatient/{id}','patientController#infoPatient');
In my controller patienController.php:
public function infoPatient($id){
$d=patientModel::find($id);
return view('/patient')->with('d',$d);
}
As a result, the view show data without any style.
the view: https://ibb.co/GCr66zk
Try This.
public function infoPatient($id){
$d=patientModel::find($id);
return view('patient',compact('d'));
}
and in your patient.blade file use variable $d to get the data
the probleme was solved, the issue was that I forget "/" in all link of bootstrap files.
Thank's.

Laravel. conflict with routes

I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.

Blade not working (Laravel), why is nothing shown?

Blade not working (Laravel), why is nothing shown?
1.show.blade.php
#extends('book.show')
#section('comment')
Yuup!
#endsection
2.book/show.blade.php
<ul class="cols">
<li>
#yield('comment')
</li>
</ul>
Nothing was wrong with the displayed code. The problem was in your routes. Here is a snippet from your routes:
Route::get('book/{book}', function () {
$comment = 'Hello';
return view('comment.show', ['comment' => $comment]);
});
Route::resource('book', 'BookController');
Route::resource('book') creates the exact same URI as 'book/{book}' so it overrides the first one. In other words, your closure is never triggered. You have several options.
Don't use Route::resource. I like to be explicit with my routes.
Put your Route::get('book/{book} after the Route::resource. This would work too.
Remove Route::get('book/{book}') and just add your code inside your BookController classes show method.
Any of these 3 options will work. I suggest option #3 if you like using Route::resource. Otherwise, I would work with option #1. Option #2 and overriding other routes and such isn't a very nice way of going about things in my opinion.
According to your code in pastebin.
Change return view('comment.show', ['comment' => $comment]); to return view('book.show', ['comment' => $comment]);
EDIT
Another problem is you end your section with #endsection. It will be #stop.
Your code should look like this:
#extends('book.show')
#section('comment')
Yuup!
#stop

Passing URL to Views in Laravel

Here is my controller code:
$view = View::make('homepage');
$loglink = URL::route('account.login');
$view->loglink = $loglink;
return $view;
Here is how I recv it in view:
<a href="{{ $loglink }}" class="red">
I get an error saying:
Undefined variable: loglink (View:
C:\BottleBookings\server\app\views\partials\header.blade.php)
Any problem with the way I am sending or recving it?
Thats not how view creation works. Just return your view and pass the variables you want to use with the with() function in the same command. Create your view like this:
$loglink = URL::route('account.login');
return View::make('homepage')->with('loglink', $loglink);
Or even shorter:
return View::make('homepage')->with('loglink', URL::route('account.login'));
Also, you don't really have to pass the link URLs, it's perfectly fine to generate them where they are needed in the view:
<a href="{{ URL::route('account.login') }}" class="red">
why you complicate. just put the datas in array and pass it to view.
Controller
$data['loglink'] = URL::route('account.login');
return View::make('homepage',$data);
p.s. your loglink variable will be available to only homepage view. NOT on header page.
if you want to pass the loglink to header, then you have to pass it as View::composer or View::share or where you are including the file. any one of the three will do.

Categories