Call to a member function checkLearning() on null (View: - laravel

hello i use this method but not worked on view
#if(! auth()->user()->checkLearning($course))
<form action="/series/payment" method="post">
{{csrf_field()}}
<input type="hidden" name="course_id" value="{{$course->id}}">
<button type="submit" class="btn-tohid">خریداری این دوره</button>
</form>
#else
به این دوره دسترسی دارید
#endif
and my model in user
public function checkLearning($course)
{
return !! Learning::where('user_id' , $this->id)->where('course_id' , $course->id)->first();
}

From what I see here, you're trying to get checkLearning() from user that is not logged in, hence you're getting the error. The auth()->user() method returns null, and null doesn't have checkLearning() method. Not exactly sure what you're trying to check in this condition, but if you want to check if logged in user has/doesn't have some records in Learning model, you need to change your condition to this:
#if(auth()->user() && !auth()->user()->checkLearning($course))
//First check if user is logged in, then check if he has/doesn't have records
#else
//Do something else
#endif
Also, I'm not sure what !! represents in your checkLearning() method, can you provide some more info what do you want to to there?

Related

Laravel collection querying

I have written a query to grab a collection of results, i've added a check to say if a record contains this field hide the record with the id of 2.
Controller method
$purchasedProducts = $user->products()->where('purchased', 1);
if ($user->products()->where('includes_bonus', 1)->first()) {
$purchasedProducts->where('benefits.id', '!=', 2);
}
$purchasedProducts->get();
Blade
in here i wrote out the foreach loop to be displayed within the blade.
#foreach($purchasedProducts as $product)
<div class="col-xl-6 p-0 p-xl-4 mb-5 mb-xl-0">
<form action="{{route('cancel.product', $product->id)}}" method="POST">
#csrf
error received
Trying to get property 'id' of non-object
<form action="<?php echo e(route('cancel.product', $product->id)); ?>" method="POST">
Can you see where i am going wrong?
You haven't assigned the results of the query to anything, you are just calling get() on the query you are building but are not doing anything with the returned results. Perhaps assign it to a variable:
$purchasedProducts = $purchasedProducts->get();

The DELETE method is not supported for this route with laravel

i'am using laravel in my project , do i want to delete an appointement , but i get this error : The DELETE method is not supported for this route. Supported methods: GET, HEAD.
This is the controller :
public function destroy($id)
{
$rdv = DB::table('rdv')->where('id',$id)->delete();
return redirect()->back()->withSuccess('success delete !' ) ;
}
}
this is the form :
#if ( $getpat->Etat_de_rdv == 'en_attente')
<td><label class="badge badge-warning"> {{$getpat->Etat_de_rdv}} </label></td>
<form method="POST" action="{{ route('delete', $getpat->id) }}">
#method('DELETE')
#csrf
<button type="submit">Supprimer rendez-vous</button>
</form>
this is the web.php
Route::get('/delete', 'rendezv#destroy')->name('delete');
It should be
Route::delete('/delete/{id}', 'rendezv#destroy')->name('delete');
You're using Route::get(), but supplying #method('delete'); those are contradictory. Modify your route as follows:
Route::delete('delete', 'rendezv#destroy')->name('delete');
Additionally, you're not passing the $id parameter, so route('delete', $getpat->id) won't work. You can do this with a form field, or a URL parameter:
Route::delete('delete/{id}', 'rendezv#destroy')->name('delete');
the correct declaration of the route is:
Route::delete('/delete', 'rendezv#destroy')->name('delete');

How To Hidden Password in URL

i dont know how hidden password on url
i got problem like this http://127.0.0.1:8000/bulletin/%201/edit?passwordC=11111&page=1
My View
<form>
<div class="form-row" style="specified-width:200; position: absolute; bottom:0; margin-bottom:10">
<input style="width:150px" type="password" placeholder="Password" name="passwordC">
<input type="hidden" value="{{$buletin->currentPage()}}" name="page">
<button style="margin:0 5px" formAction="/bulletin/ {{ $tampil_B->id }}/deleteOper" type="submit" class="btn btn-danger">Delete</button>
<button formAction='(url(edit))' type="submit" class="btn btn-primary">Edit</button>
</div>
</form>
My Router
route::get('/bulletin/{id}/edit','BulletinController#edit');
my controller
public function edit (Request $request, $id)
{
$buletin = \App\Dashboard::find($id);
$url = "/?page={$request->page}";
if(is_null($buletin->password)){
$request->session()->flash('failed', 'Cant Edit Because this post not had been set password ');
return view('bulletin.edit_nopass', ['buletin' => $buletin,'url'=> $url]);
}
if (hash::check($request->passwordC,$buletin->password)){
return view ('bulletin.edit', ['buletin' => $buletin, 'url'=> $url]);//save and go back to card
} else {
$request->validate([
'passwordC' => 'required|required_with:password|same:password'
],[
'passwordC.required_with' => "Password not match",
'passwordC.required' => "Password Required",
'passwordC.same' => "The Password You Entered Do Not Match.Please Try Again"
]);
}
The issue is a byproduct of how you have written this solution. To remove the password from the URL, you will have to find a different mechanism to get to the edit page.
As it currently stands, you are doing a GET request to the edit page from the form, and because it is a GET request, the form parameters are sent in the URL.
From the edit controller method you are then returning a view, so the URL is never re-written.
That is why you have this problem, as to how you could solve this, there are many options; you could post to an endpoint that stores the approval in a session that you then check in middleware, or in the controller, and then return the view. You could use the reconfirm password middleware from Laravel. Or even a POST-REDIRECT-GET pattern, where you post the form and then redirect to the edit page from there with whatever you need to do to protect the edit endpoint.
There are many options, but its impossible to tell you how to solve this problem given that you need to rethink how you will solve it.
First of all it is not correct to send with GET .But if it is very vital you have two way:
1.use encrypt .but it is not safe too.because there is even online sites that can decrypte .
2.use Hash:make . Hashing is an unilateral.It means that you can not dehash it

Laravel redirect()->with('status','My message') error

I have created a email verification on Laravel 5.2. When a user fill the registration form and is validated they return the user to login form with:
return redirect('/login')->with('status','We send a email verification, please
confirm.');
And this works perfect. The problem is when the user click on link verification in his inbox they activated the account and redirect to admin page but without status message. Here is the code for that in AuthController.php:
public function activateUser(Request $request,$token)
{
if ($user = $this->activationService->activateUser($token)) {
return redirect('/login')->with('status','You account is now activated. Please login.');
}
abort(404);
}
On my login.blade.php:
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
Please, someone know why the session variable is not working?
Thanks.
it should work like this. in your AuthController, just change
return \Redirect::to('/login')->with('status','You account is now activated. Please login.');
and in your login.blade
#if (Session::has('status'))
<div class="alert alert-success" role="alert">
<p>{{ Session::get('status') }}</p>
</div>
#endif
Okey i don't what happend here and why redirect()->with() is not working. I solved doing a new view called activated.blade a copy of login.blade but with a message saying: "You account is now activated. Please login." so i do this in the controller:
return redirect('/activated');

Add Checkbox to database in Laravel 5

Im not 100% great in html and databases but I'm getting there. What I did works but not when I'm editing a page that has checkboxes.
In my database migration:
$table->string('check_this')->nullable();
In views:
<div class="checkbox">
#if ($job->check_this == 'selected')
<label><input type="checkbox" name="check_this" value="selected" checked>Yes.</label>
#else
<label><input type="checkbox" name="check_this" value="selected">Yes.</label>
#endif
</div>
Once the checkbox is checked, it writes to the db so my if statement wont work correctly. Is there a way that if unchecked it deletes the value from the database?
Maybe I have go about this is the wrong way as in the correct format for checkboxes in a migration.
Your migration should looks like this:
$table->boolean('check_this');
and the view part:
#if ($job->check_this)
See: http://laravel.com/docs/master/schema#adding-columns for all avail types of 'columns' in Laravel.
Checkbox don't send nothing if is not cheched so the database will not update unless you check in controller and if "check_this" is not set in your request set this to 0 on database.
$obj = Obj::find($id); //get your object from database
$input = Request::all(); //or $request->all() if you use requests
if(!isset($input['check_this'])){
$input['check_this'] = 0; //or whatever you want
}
$obj->fill($input);
$obj->save();
Note:
You can simplify your view
<div class="checkbox">
<label><input type="checkbox" name="check_this" value="selected" #if ($job->check_this == 'selected') checked #endif>Yes.</label>
</div>
And of course the answer about using boolean for this field will optimize your database if you don't need this specific string.

Resources