I would like to send a parameter from view to controller.
How to do this in the views blade and controllers ?
DB::table('news')->where('id', '=', 1)->get()
So everything works. But I would like it to look nicer. e.g
{{ $news->id='1' }}
This is my beginnings with laravel.
I'ld suggest you try sending data to controller via route as mentioned in other answers. But if you still don't want route, try this method.
Make your controller function as static. Eg:
public static function newsFunc($id){ //** static
$data = DB::table('news')->where('id','=', $id)->get();
.
.
.
.
.
}
In your view call
{{ \App\Http\Controllers\YourControllerName::newsFunc($news->id) }}
It doesnt look like a good idea to send any data FROM view TO controllers
But if you really need it, you can pass controller object to view and interact with it in blade file:
class CatalogController extends Controller
{
public function showNews()
{
return view('catalog.sections', [
'controller' => $this,
]);
}
}
your blade file:
<div>
#php($controller->doSomething())
</div>
In your Blade template you can call a route like:
<a href="{{route('something',$news->id)}}"
And in your route file you declare this route:
Route::get('something/{id}','yourController#yourMethod');
In your controller you can retrieve it:
public function yourMethod($id){
//here u can use $id
}
So this is something that you could do with another route.
So you can register your route like this:
Route::get('/news/{id}', 'YourController#getNews');
And then your controller would have a method:
public function getNews($id) {
$news = DB::table('news')->where('id', '=', 1)->first();
return view('your-view', compact('news'));
}
And in your view to pass the id you would have something like:
Go to first
Hope that helps!
Related
i have tried to passing data to multiple blade in controller but get error. Here bellow my code
public function index()
{
$news = DB::table('beritas')
->select('id','judul_berita','created_at')
->get();
return view (['berita.daftar-berita', 'more-menu.berita'])->with(compact('news'));
}
How to pass data to multiple blades in laravel and with a single route?
If u want pass data to multiple blades u can share it in the Constructor like so:
public function __construct(){
$this->middleware(function ($request, $next) {
$news = DB::table('beritas')>select('id','judul_berita','created_at')->get();
View::share('news', $news);
return $next($request);
});
}
and now u can use news variable in all you blades that using the same controller.
i hope it's will help you
Probably the right place to this is in the boot method of some service provider, for example, AppServiceProvide.
//AppServiceProvider.php
public function boot()
{
view()->share('someVariable',$someVariable);
}
This will make someVariable available to all of blade views. This is useful for template level variables.
I am using Laravel AdminLTE and I have it all configured, there is just one part I do not understand. I made my route like so:
Route::get('/admin/painlevel', function () {
return view('painlevel');
});
and I have this method in app/Http/Controllers/v1/PainLevelController.php
public function index()
{
return PdTpainlevel::select('pkpainlevel as id', 'painlevel_name as name')->get();
}
How would I call that method and display the data in my painlevel view?
Your current route is merely returning the view('painlevel') directly.
You need to update your route to:
Route::get('/admin/painlevel', 'V1\PainLevelController#index');
In your controller:
public function index()
{
$data = PdTpainlevel::select('pkpainlevel as id', 'painlevel_name as name')->get();
return view('painlevel', compact('data'));
}
You might want to start glancing through the documentations, start with Route, Controller and View
route create like this
Route::get('/administrator', 'administrator\LoginController#index');
and controller create like this
public function index()
{
$data['title']="Admin | DashBoard";
$data['name']="Dilip Singh Shekhawat";
view('administrator/menu_bar',$data);
return view('administrator/dashboard',$data);
}
its working .
I am very obviously a noob to Laravel and hope that someone can help me out.
The about screen is accessed through the route
Route::get('/about', array('as' => 'about', function()
{
return View::make('about')->with('title','About Screen')->with('class','about');
}));
The variables $title and $class are accessible in about.blade.php by {{ $title }} and {{ $class }}. If instead, I have a Controller in between,
Route::get('hello/create', array('as' => 'create', 'uses' =>
'HelloController#create', function()
{
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}));
How do I access $title and $class in the HelloController.php code (so that I can propagate the values to the coming View)?
P.S. I do know about the /hello/create/{name of variable} which is the answer on nearly all questions similar to this, but don't know how to use it to transmit variables NOT keyed onto the Http Request.
$title and $class are the values you are manually giving to the blade. These aren't the values that you are receiving in GET parameters in your route. So, you would do it the same way as you did in the closure.
Your route:
Route::get('hello/create', array('as' => 'create', 'uses' => 'HelloController#create'));
Controller method:
class HelloController{
public function create(){
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}
}
UPDATE:
From what I understood, you can also call controller's method inside the route's closure and pass parameters to the controller and call the view with these values inside the controller's method.
Your route file:
use App\Http\Controllers\HelloController;
Route::get('hello/create',function(){
$hello_obj = new HelloController();
return $hello_obj->create('create','hello.create');
});
Controller method:
class HelloController{
public function create($title,$class){
return View::make('hello/create')->with('title',$title)->with('class',$class);
}
}
First you need to clear your flow. You are -at the moment- manually setting the variables to be returnet to the view, so your route should look like this:
Route::get('hello/create', 'HelloController#create');
Then, your controller handles the logic:
public function create(Request $request)
{
return view('hello.create')->with('title','Create')->with('class','hello.create');
}
Now, if you need to send parameters from your frontend to your controller, you have two options:
Define route parameters.
Use query params.
Option 1
For the first option, you'll need to define your required/optional parameters in the route itselft:
Route::get('hello/create/{a_variable}', 'HelloController#create');
Then you access this parameter in any of this ways:
public function create(Request $request)
{
return view('hello.create')->with('a_variable', $request->a_variable);
}
or injecting the variable in the method:
public function create(Request $request, $a_variable)
{
return view('hello.create')->with('a_variable', $a_variable);
}
Option 2
For the use of query params, you should include this options when making the request. If your route looks like this:
Route::get('hello/create', 'HelloController#create');
You could specify query params like this:
GET www.my-domain.com/hello/create?first_parameter=value_1&second_parameter=value_2
So in your controller you access this values like this:
public function create(Request $request)
{
$value_1 = $request->get('first_parameter');
$value_2 = $request->get('second_parameter');
return view('hello.create')
->with('value_1', $value_1)
->with('value_2', $value_2);
}
You are alreading sending data to view using with().
Echo it in your view file using $variablename set in with() Example: <?php echo $title; ?>
This might be super simple but I'm asking because I'm new to this.
I have a View named 'defaultpage' , I have two routes and one controller as below.
Route::get('about', 'PageController#showPage');
Route::get('welcome', 'PageController#showPage');
.
class PageController extends Controller
{
public function showPage(){
return view('defaultpage', compact('var'));
}
}
I want to pass a variable named $var from above routes to below controller , If $var is coming from 'about' route , $var should be equal to a value given in about route and the controller will pass it to the view , and same with the 'welcome' route.
How can I do this . Any help will be appreciated . Thank you .
Easiest way to pass variable from route to controller if the varible is coming from view is to put it a link as below
Click me
In routes:
Route::get('myRoute/{variable}', 'ControllerName#getFunction');
In Controller
public function getFunction($variable){
echo $variable;
}
I have simple function in one of my controller (members):
function action_data($array) {
$datamembre = DB::table('members')->where('id', '=', $id)->first();
return $datamembre;
}
I want use it in the view of another controller, I do this in my template:
$datamembers = Controller::call('members#data', array($members_id));
Is there any better or proper way to do it?
The best would be to put it into a Member model IMO.
Use view composer http://laravel.com/docs/views#view-composers
View::composer(array('home', 'profile'), function($view)
{
//
});
update
You can put the method in BaseController, all controllers extend it, so you could get this method in any controller/view. And make it static.
// in BaseController.php (L4) or Base.php (L3)
static public function action_data($array) {
$datamembre = DB::table('members')->where('id', '=', $id)->first();
return $datamembre;
}
In the view
BaseController::action_data($array); //L4
Base_Cotroller::action_data($array); //L3
Hope it works. Haven't tested it.