Error even before controller method has been invoked in Laravel - php

I have the following code in my view:
#if(Session::has('quality-data'))
Submited Quality Check
#endif
Now when I open this view in the frontend I get the following error:
The method that's invoked when clicked on the a tag is the following:
public function showQualityResult($qualityData) {
return $qualityData;
// return view('quality-result' , compact($qualityData));
}
Now even though I have not clicked on the a tag I am still getting an error that the parameter for the function hasn't been passed. Why is this?
Just the fact that I have the below lines of code in my view:
#if(Session::has('quality-data'))
Submited Quality Check
#endif
Makes my application not work.

I think your error is due to the usage of compact .. that does not match with the expected data required.
Indeed, your route require the data argument. Compact will create an array where the key the string in parameter, and the value associated.
Is Session::get('quality-data') return 'data' ? And $data exists ?
So, you should write:
#if(Session::has('quality-data'))
Submited Quality Check
#endif
If this works ... so, you will be able to simplify the writing.

Related

how to use DD() method in Laravel projects?

I know that for some it might be stupid or funny question (but I am newbie) but I need to find know how to properly use DD() method in laravel projects.
For example - I have got tasks to debug some code and functionality in my project (PHP laravel). And it always takes me for ever to find the exact file or folder or code where the problem is.
My mentor says to use DD() method to find things faster (but for learning purposes he didn't explain me a lot about how to actually use it and said to find out my self), but said that I should start with Route (we use backpack as well for our project). So after finding Route (custom.php file) which controller connects to my required route what should I do next? How do I implement dd() method (or as my mentor says dd('call here') method) to fast find what I should be looking for to solve my problem and complete my task? Where should I write this dd() and how should I write it?
Thank you for the answer in advance!
for example I have a:
public function create(): View
{
return view('xxxxxx. \[
//
//
\]);
}
and if I put dd() anywhere in the code, I get error message in my URL :(
first of all ,in Laravel we use dd() before return in order to read any variable.
in controller we often use two kinds of variables : collection(which we get its members via foreach) or singular variable (we get it via its name)for example:$var = 1; dd($var).
notice:
if you are using ajax response you will not be able to see dd() results in page ,you can see the result via network tab in your browser (if u inspect your page).
dd stands for "Dump and Die."
Laravel's dd() function can be defined as a helper function, which is used to dump a variable's contents to the browser and prevent the further script execution.
Example:
dd($users,$variable1,$var2);
You can use dd() in blade
#foreach($users as $user)
#dd($user)
OR
{{dd($user)}}
#endforeach
#dd($var1)
You can read this article, the have more example and comparison
https://shouts.dev/articles/laravel-dd-vs-dump-vs-vardump-vs-printr-with-example
As Laravel is following model-view-controller or MVC design pattern. First go to the route and check which controller is called in the URL with the related URL.
Then go to the controller. **dd**() function is basically a dump and die. you also can do this by **print** or **echo** function too.
Lets assume that I have a controller name ProductController where I have method name index.From where I need to show a list of products in a table.
// in controller
public function index()
{
$products = Products::all();
// here you think ,I need to check whether I am getting the output or
not.
dd( $products );
//Or echo $products;
return view ('product.list',compact('products'));
}
let's suppose you are getting everything but in view when you loop through the products you declare the wrong variable name or mistakenly do some spelling mistakes. and want to see the result.
In view just do the dd() method by the following way:
{{ dd($products) }}

Laravel : form does not change the URL after submit, causing me to be unable to do another POST in web.php

I have a bit of a complicated issue. I could use some help.
I have a form that is being handled by the following function:
$module = request('module');
$classe = request('classe');
$horaire = request('horaire');
$date = request('date');
$students = DB::select('SELECT * FROM `etudiants` WHERE etudiants.id_classe = '.$classe);
return view('g_absence.absence',['module'=> $module, 'classe'=>$classe,'horaire'=>$horaire,'date'=>$date,'students'=>$students]);
I take the values $module, $class, $horaire, $date and $students and need to use them inside a different view: g_absence.absence. This works fine and when the view is returned I have access to said variables.
The issue is, inside the g_absence.absence view, I have another form that also needs to be handled, and because the url remains the same even tho a different view is returned, I cant make two posts for the same path.
web.php:
Route::get('/testboy', [App\Http\Controllers\g_absence::class,'index'])->name('marquer');
Route::post('/testboy',[App\Http\Controllers\g_absence::class, 'marquer']);
Route::post('/testboy',[App\Http\Controllers\g_absence::class, 'ajoutabsence']);
The first line is the one that send to the form page just a simple
return view
The second one handle the form in that view
The third one, I want it to handle the form inside the
g_absence.absence view, but they share the same path.
Excuse me if I'm being unclear, I'm a bit of a beginner in Laravel
your problem is using the same route for different methods
basically the first route gets executed every time you use the '/testboy' action that is why your second function never get's called.
you can solve this issue by changing your urls for example:
Route::post('/testboy-marquer',[App\Http\Controllers\g_absence::class, 'marquer']);
Route::post('/testboy-ajoutabsence',[App\Http\Controllers\g_absence::class, 'ajoutabsence']);
Or you can use one function that's handle both with one url by pathing additional parameter to your url depending on your function call :
Route::post('/testboy?type=marquer',[App\Http\Controllers\g_absence::class, 'ajoutabsence']);
in your function check the type :
if(request('type') == 'marquer') {
execute marquer logic here...
} else {
execute absence logic here...
}
Using method and path with two functionalities is wrong, but if you want to somehow use both routes my same method and path which I don't recommend you must let the request to pass through like a middleware in your first block of code Instead of return a view.
Recommended way is to have 2 routes with different paths or at least one route with a parameter by which you can determine what code block must be executed.

Parameter not being passed to controller method in laravel when using route method

I have the following code in my anchor tag in my laravel application:
Submited Quality Check
My route is set to run the following method when clicked on the above link:
public function showQualityResult($qualityData) {
return $qualityData;
// return view('quality-result' , compact($qualityData));
}
Now when i click on the link, i get the following error:
Why am i getting a Missing argument 1 error when i clearly am passing data as a parameter in the tag like so below:
Submited Quality Check
Why is the parameter not being passed to the method ?
Your route from your previous question:
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult');
To pass the parameter, you should define it. So, it'll look like this:
Route::get('showQualityResult/{data}', 'QualityCheckController#showQualityResult');
And showQualityResult() method should accept it:
public function showQualityResult($data)
{
....
I'd recommend you to read the docs to understand how it works.

Laravel 5 variable not being recognised

Bit of an odd problem which is probably because I am finding my feet with Laravel.
I have an edit page which is accessed by:
Route::get('editRow/{id}', function($id){
$section = App\Section::where('id', $id)->with('panels')->first();
return view('front.editrow',['thesection'=>$section]);
});
This form then is completed and goes to a controller with a longish method to update the table and ends with:
return view('front.editrow',['message'=>'update successful','id'=>$request->id]);
The DB is updated OK. Initially I was getting an error so I copied the route and changed it from a get to a post:
Route::post('editRow/{id}', function($id){
$section = App\Section::where('id', $id)->with('panels')->first();
return view('front.editrow',['thesection'=>$section]);
});
Now although this is exactly the same parameters etc I always get;
Undefined variable: thesection
I am baffled and would greatly appreciate this mystery being solved!
Make sure that thesection variable is always passed to the view.
When you do:
return view('front.editrow',['message'=>'update successful', 'id'=>$request->id]);
this variable is not provided. You need to either provide it in the view or check for its existence in the blade template with:
#if(isset($thesection)
// whatever you want to do with the section
#endif

Unable to route to second edit resource Laravel-4

I have a resource controller for member data. All of the usual resource functions, including edit, are working perfectly. I am trying to add additional edit functions within this controller so that I can create views that only are for specific subsets of the Member model data, since the data set is rather large. So, I've set up the extra routes and functions. But when I attempt to link to the edit2 resource, Laravel will not create the proper link. I don't know what I'm doing wrong. Code:
Route:
Route::get('members.edit2', array('as'=>'edit2', 'uses'=> 'MembersController#edit2'));
Route::resource('members','MembersController');
MembersController:
// Regular edit function -- works just fine:
public function edit($id)
{
$member = $this->member->find($id);
return View::make('members.edit', array(
'member'=>$member, ...
));
}
// Extra edit2 function -- should work if I could successfully route to it:
public function edit2($id)
{
$member = $this->member->find($id);
return View::make('members.edit2', array(
'member'=>$member, ...
));
}
show.blade.php:
// normal edit link (works fine, see source code below):
edit
// additional edit2 link (creates a bad link, see source code below):
edit
source code:
// normal link that uses edit for member id=27:
edit
// link that attempts to use edit2 for same member:
edit
I'm sure there is a way of doing this. It doesn't matter whether I use the named route 'edit2' rather than 'members.edit2', the exact same bad link is created. I've tried every combination I can think of. Laravel docs are not at all helpful for this. Thanks!
Your don't declare your edit2 route as you should do. Your first mistake is that the member's id you want to edit is not passed as a parameter and the second one that by calling this {{route('edit2')}} Laravel expects a url like /members.edit2 which is never going to appear. You should better use sth like /members/{id}/edit2.
Try using this:
Route::get('members/{id}/edit2', array('as'=>'edit2', 'uses'=> 'MembersController#edit2'));
and call it like:
{{ route('edit2', [$id]) }}
Also be careful, whenever you call Url::route() or simply route() you should pass their parameters in an array like:
{{route('myRoute', ['par1', 'par2', 'par3', ...]}}

Categories