« first day (40 days earlier)      last day (308 days later) » 

1:08 AM
hi
pls everytime i try to run this query: imgur.com/a/TTrcMzy
i get the following error: imgur.com/a/FEuGW8x
can anybody help me?
 
I'm trying to learn react front end dev, as far as i am aware this means i have to create some api on the back end which will be consumed by react? Do i need to implement jwt?
 
1:25 AM
in my opinion, i think you only need to implement jwt if you're creating an API that will allow interaction from external clients (i.e others people code)
@MichaelStrobel I've worked with laravel and reactjs twice before. I didn't implement anything like jwt, all i did was modify the controller to return response in json format, create a route for each controller method that returns a response, and then use axios or javascript fetch() api to access the route and get the response.
 
@IsraelObanijesu So i can use regular authentication/guard in php? and just return a json response if user is authenticated?
 
Thanks. :)
 
 
4 hours later…
5:15 AM
Hello Sir @ZakariaAcharki
As you said delete option putting with another actions is not a good idea
So I decided to put it somewhere as a button
Now After clicking that button. It opens a popup for confirmation and then from that popup its going to the destroy method
But the only thing I am struggle with is that its not doing anything but just redirect back
So I do dd($request->all();)
And try to see if I am getting the selected checboxes
But its giving me this array
array:2 [▼
"_method" => "DELETE"
"_token" => "uTc5lLJw7iHQo1l4q5biSR52cv1XJsNVslnbkzCo"
]
I thing I am not getting the selected checboxes
Can we please discuss it when you have time ?
Button
<div class="form-group">
<a data-toggle="modal" data-target="#deleteInvoiceModal">
<button class="btn btn-danger float-right fas fa-trash-alt"> Delete</button>
</a>
</div>
Modal
@foreach($invoices as $invoice)
<div class="modal fade" id="deleteInvoiceModal">
<div class="modal-dialog modal-md">
<div class="modal-content">

<div class="modal-header bg-danger text-white">
<h5 class="modal-title">Delete Confirmation</h5>
<button class="close" data-dismiss="modal">
<span class="text-white">&times;</span>
</button>
</div>
<div class="container">
<div class="modal-body">
<div class="col-md-12">
<p class="text-center">Do you really want to delete ?</p>
{!! Form::model($invoice, ['method'=>'DELETE', 'action'=>['FeeController@destroy', $invoice->id]]) !!}
Controller
public function destroy(Request $request)
{
dd($request->all());
}
 
 
3 hours later…
8:07 AM
Hi bro
it's not working because you've no checkboxes in the form that you're submitting
the checkboxes are in another form
so you may need to submit the delete form using ajax instead
 
 
1 hour later…
9:11 AM
{!! Form::open(['method'=>'GET', 'action'=>'FeeController@invoicesActions']) !!}

<div class="form-group">
<a data-toggle="modal" data-target="#deleteInvoiceModal">
<button class="btn btn-danger float-right fas fa-trash-alt"> Delete</button>
</a>
</div>

<div class="form-group float-right d-flex">
<select name="options" class="form-control bg-dark text-white">

<option value="select">Select Action</option>
<option value="print">Print Invoices</option>
<option value="pdf">Generate PDF</option>
<option value="delete">Delete</option>
This form I need to submit using ajax ?
Or the modal form ?
Sorry sir. It's my exam started in university that's why I am replying late
 
 
2 hours later…
11:20 AM
@ZakariaAcharki Ok sir I have ajax now for submitting form
Can you please review ti if I am doing right or not
$(".deleteInvoice").click(function(){
var id = $(this).data("id");
var token = $(this).data("token");
$.ajax(
{
url: "invoice/delete/"+id,
type: 'PATCH',
dataType: "JSON",
data: {
"id": id,
"_method": 'DELETE',
"_token": token,
},
});

});\
Controller
if (!empty($request->checkBoxArray)) {
Fee::whereIn('id', $request->checkBoxArray)->delete();
return response()->json();
}
In my controller I have this code but its not working and just redirecting to the same page
 
11:41 AM
don't worry and good luck in your exams
first, you must always keep the dd until you receive what you want from the request
so now just go with dd( $request->all() ) until you get the checkboxes
$(".deleteInvoice").click(function(){
var action = $(this).prop("action");
var token = ???;
$.ajax(
{
url: action,
type: 'PATCH',
dataType: "JSON",
data: {
"_method": 'DELETE',
"_token": token,
},
}).done(function( data ) {
alert( 'success' );
});
});
notice the token var token = ???; I don't know where you've that token
it must be in the meta like : <meta name="csrf-token" content="{{ csrf_token() }}">
if that is the case so replace the "???" by $('[name="csrf-token"]').prop('content')
then we need to add the checkboxes to the ajax request
I need to see an example of a generated table with checkboxes
to see how we'll get them else we can give the main form that contains those checkboxes an id
and get all the form elements values using $('#form_id').serialize()
then filter just checkboxes and send them with our "_method" and "_token" in the "data" object part
_______________________________________
@IsraelObanijesu have you figured out which of the 3 eloquent expressions that give this error?
 
 
2 hours later…
2:13 PM
@ZakariaAcharki Yes, the last one
 
Yes sir I have that token in the HTML header.
So replaced it like this
var token = $('[name="csrf-token"]').prop('content');
Now what you wanna see ? The compiled html code for table ?
Compiled html
<table id="studentsData" class="table table-striped table-bordered">
<thead>
<tr class="filters">
<th class="align-middle text-center"><input type="checkbox" id="options"></th>
<th class="text-center">Invoice ID<input type="text" class="form-control" disabled=""></th>
<th class="text-alignment text-center">Student ID<input type="text" class="form-control" disabled=""></th>
<th class="text-center">Student Name<input type="text" class="form-control" disabled=""></th>
<th class="text-center">Total Amount<input type="text" class="form-control" disabled=""></th>
Is it correct ? let me know
 
@HasnainKahn "content" is an HTML attribute not a property
you should use .attr() method instead
 
2:55 PM
@HasnainKahn yes, now place $('[name="checkBoxArray[]"]').val(); after the token variable declaration and check what is the output
@IsraelObanijesu what is the output of dd($messages_to->toArray()); ?
 
3:32 PM
ok, i will let you know when i do that. Right now, im far away from the code. Thanks for helping me tho
 
4:15 PM
var token = $('[name="csrf-token"]').prop('content');
$('[name="checkBoxArray[]"]').val();
@ZakariaAcharki Ok I did that like this
And I have the dd($request->all()); in my controller
array:2 [▼
"_method" => "DELETE"
"_token" => "nYPxPPnUBKdD5vUUcbTCpFBLdzPgWw2ibwNvYsBs"
]
This is the output
And let me show you my popup for the delete
Modal
@foreach($invoices as $invoice)
<div class="modal fade" id="deleteInvoiceModal">
<div class="modal-dialog modal-md">
<div class="modal-content">

<div class="modal-header bg-danger text-white">
<h5 class="modal-title">Delete Confirmation</h5>
<button class="close" data-dismiss="modal">
<span class="text-white">&times;</span>
</button>
</div>
<div class="container">
<div class="modal-body">
<div class="col-md-12">
<p class="text-center">Do you really want to delete ?</p>
{!! Form::model($invoice, ['method'=>'DELETE', 'action'=>['FeeController@destroy', $invoice->id]]) !!}
 
4:38 PM
Hi
Hasnain it must be
console.log( $('[name="checkBoxArray[]"]').val() );
 
5:18 PM
array:2 [▼
"_method" => "DELETE"
"_token" => "nYPxPPnUBKdD5vUUcbTCpFBLdzPgWw2ibwNvYsBs"
]
same result sir @ZakariaAcharki
Here in the ajax I have type PATCH. I think this would be delete
 
@ZakariaAcharki after running dd($messages_to->toArray()); I got this: imgur.com/a/D0QC7jv
 
5:43 PM
I think i understand the problem now, so the first two queries returns an array of objects whereas "whereIn" expects an array, so i have to convert them to array in order for the third query to work. The question now is that: How do i convert them to an array. I tried using $messages_to->toArray() and (array)$messages_to, none of them worked. Do you have any idea?
 
6:14 PM
I guess instead of select('from')->get(); you need pluck('from')->toArray()
@HasnainKahn console.log() will return the output in the console
I want you to check it instead of dd() output to see what it will give us
if it's returning the correct selected checkboxes then we'll send it in the data object and receive it in the controller
 
6:29 PM
hmm, that works but now i have a new problem
after retrieving the users, i created a method that iterates through the list of users, get their last msg, and append it to the user collection. This method was working before until now when i changed the select('from')->get() to pluck('from');
Here is the method: imgur.com/a/QujQjUs
and here is the error thrown by the method: imgur.com/a/eS4SQSS
Please, how can i solve this? I've never worked with pluck before
 
Try $user->last_msg instead of $user['last_msg']
 
wow, that works
please can you explain why it changed from an array to object
and what does pluck() do that makes it work
 
not sure I don't know from where you get the $users
I just noticed that $user is an object
since it's already working with $user->slug
 
yeah, but the thing is
slug is already part of the $user by default
but last_msg is not, so in order to add it since it is new, i normally specify it as an array key
im still a little bit confused with that part, but pls can you explain what pluck() does
 
 
4 hours later…
10:20 PM
Pluck is a function that get an array with the values of a given column.
140
A: Eloquent ORM laravel 5 Get Array of ids

Zakaria AcharkiYou could use lists() : test::where('id' ,'>' ,0)->lists('id')->toArray(); NOTE : Better if you define your models in Studly Case format, e.g Test. You could also use get() : test::where('id' ,'>' ,0)->get('id'); UPDATE: (For versions >= 5.2) The lists() method was deprecated in the ...

 

« first day (40 days earlier)      last day (308 days later) »