Laravel collection querying - laravel

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();

Related

Laravel 8 multi value select filtering

I'm trying to build Laravel project that will have a multi-select dropdown with list of categories. Selecting category should reload the page and apply filter.
"scopeFilter" in my model is actually doing filtering, but here i just need to find a way to properly form URL. The problem is that this code i have:
<form id="cats-form" action="#" method="GET">
<select multiple class="chosen-select" name="test[]">
#foreach($categories->get() as $cat) //this loop goes through existing categories in the system
#php
//if category is part of URL, pre-select it in the select:
$arr = request()->all();
$selected = '';
if(array_key_exists('test', $arr)) {
$testArr = $arr['test'];
$selected = in_array($cat->id, explode(',', $testArr)) ? 'selected' : '';
}
#endphp
<option {{ $selected }} value="{{ $cat->id }}">{{ $cat->name }}</option>
#endforeach
</select>
</form>
<script>
$(".chosen-select").chosen({ })
$('.chosen-select').on('change', function(evt, params) {
$('#cats-form').submit();
});
</script>
Is actually giving me this URL:
http://localhost:11089/?test%5B%5D=19&test%5B%5D=5
While i actually need this:
http://localhost:11089/?test=19,5
I guess it's a trivial problem to solve for someone with better knowledge of Laravel, can you tell me pls what i'm doing wrong here?
This is rather how the language or framework or library reads an URL query string. Assuming that your prefered way of URL string is a valid format (as I usually see the format ?arr[]=val1&arr[]=val2 and never see the second format that you preferred), both query string formats should be acceptable to PHP.
As Yarin stated that the best way PHP reads an array is the first method, you don't have to worry too much because the decoded URL query string is exactly in the first format.

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

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?

Laravel Livewire key() expects parameter 1 to be array, integer given | nested components | loading a component inside a loop

I've been working with Laravel livewire for a while now, I've a nested components, which is product list for my site and inside that list I've another component for adding product to wishlist. As per documentation stated here , it says
"Similar to VueJs, if you render a component inside a loop, Livewire has no way of keeping track of which one is which. To remedy this, livewire offers a special "key" syntax:"
Like this:
<div>
#foreach ($users as $user)
#livewire('user-profile', $user, key($user->id))
#endforeach
</div>
Here is my code snippets from my project.
<div>
#foreach($products as $product)
<div class="product-box white-bg mb-8" data-dusk="product">
{{-- here im passing product id as param in key(), 'productList' is a static value for a variable of mount(). --}}
#livewire('desktop.wish-list-add', $product, key($product->id), 'productList')
<div class="product-content d-flex justify-content-between align-items-center p-5">
...............
#endforeach
{{ $products->links() }}
</div>
The issue is when I try to pass $product->id as param for key(), it gives error
key() expects parameter 1 to be array, integer given
But the doc clearly shows that we have to pass id as param. Has anyone faced this issue so far?
#livewire('photos.photo-wire', ['photo' => $photo], key($photo->id))
instead of
#livewire('photos.photo-wire', ['photo' => $photo, key($photo->id)])
because that will generate the error:
key() expects parameter 1 to be array, integer given
okay, I found the solution ( however it doesn't make sense to me , but it works :/ )
You have to pass other parameters for mount() like this:
#livewire('desktop.wish-list-add', 'productList', $product->id, key($product->id))
Instead of this:
#livewire('desktop.wish-list-add', $product, key($product->id), 'productList')

How to send a value to route in laravel

I want to send a value to route that i am received from the controller.!How can i do it.
My controller:
public function payment_history($id)
{
$payment_details=AmountDrvPaidHistoryModel::where('driver_id',$id)->get();
return view('admin.driver_payment_history')->with('payment_details',$payment_details)->with(array('id'=>$id));
}
Here is my blade:
<form action="{{URL::to('driver_payment_history/'.$id)}}" method="post">
How can i get the value of id in action tag that is send to another route.
just remove array from this
->with('id'=>$id);
You code will be:
public function payment_history($id)
{
$payment_details = AmountDrvPaidHistoryModel::where('driver_id',$id)->get();
return view('admin.driver_payment_history', compact('payment_details', 'id'));
}
With compact you can get the variable data, so $id will pass on to the view.
Than you can get the id with this code:
<form action="{{ url('driver_payment_history/'.$id) }}" method="post">
EDIT
<form action="{{ url('driver_payment_history/'.$payment_details[0]->driver_id) }}" method="post">
Hope this works!
You have to reference the $id inside your function, initiating it to be passed by the specific function.
public function payment_history($id) //<-this is the parameter passed into the function
{
$payment_details=AmountDrvPaidHistoryModel::where('driver_id',$id)->get();
$new_var_id = $id; //<- Assign the $id to a new variable (eg. $new_var_id)
return view('admin.driver_payment_history',compact('payment_details','new_var_id');
}
You will then access the variable as such
<form action="{{ url('driver_payment_history/'.$new_var_id) }}" method="post">
However keep in mind that you are already doing a get request with the $id variable, and sending all the results to the page. This means that you can just access the specific id in the view by accessing the objects properties without re initiating the $id variable.
<form action="{{ url('driver_payment_history/'.$payment_details->driver_id) }}" method="post">
If you do use this, you can remove $new_var_id(or whatever you named it) from your controller, as well as removing it from compact()

laravel 4 how to show result at the same page and how to solve circle invoke

I am totally new to laravel, I am now want to use laravel 4.
Suppose I have a page A.php, and it contains a form & a submit button. After I submit the post request to B.php, and in B.php I query data from database.
My question is I want to show my result on B.php , that it is to say the same page of A.php request, how do I write in routes.php.
My code:
master.blade.php
<!DOCTYPE HTML>
<html>
<head>
<metacharset="UTF-8">
<title>course query</title>
</head>
<body>
<div class="container">
#yield('container')
</div>
<h1 class="ret">The result is:
#yield('ret')
<input id="result" type="text"/>
</h1>
</body>
</html>
A.php
#extends('course.master')
#section('container')
<h1>My Courses</h1>
{{Form::open(array('url' => 'csOp'))}}
{{Form::label('course', 'Your course')}}
{{Form::text('course')}}
{{Form::submit('Submit')}}
{{Form::close()}}
#endsection
routes.php
Route::get('course', function(){
return View::make('course.B');
});
Route::post('csOp', function(){
// do something
//$inputCourse = Input::get('course');
//$records = Courses::where('name', '=', $inputCourse)->get();
// how do I return
//return View::make('csOp', $records);
});
As you can see, in A.php I have a form and request to csOp
Form::open(array('url' => 'csOp')
csOp is B.php, and in B.php I query data from db, and now I got the results,
but how can I put result to the page (B.php) itself? That it is to say I want to put the result to
<input id="result" type="text"/>
You know in jquery is easy, how do I use it in laravel 4 ?
And if return to csOp, absolutlly will get an error, it is in a circle. So how can I solve it ?
Thanks very much.
If you want to populate a form based on model contents, i,e. populate form using database data.
So in laravel you can use Form Model Binding. To do so, use the Form::model method. so in your case
Route::post('csOp', function(){
// do something
$inputCourse = Input::get('course');
$records = Courses::where('name', '=', $inputCourse)->get();
// how do I return
return View::make('csOp')->with('records',$records);
});
And your csOp.blade.php
#extends('course.master')
#section('container')
<h1>My Courses</h1>
{{Form::model($records,array('url' => 'csOp'))}}
{{Form::label('course', 'Your course')}}
{{Form::text('course')}}
{{Form::close()}}
#endsection
Now, when you generate a form element, like a text input, the model's value matching the field's name will automatically be set as the field value. So, for example, for a text input named course, the Courses model's course attribute would be set as the value.

Resources