laravel ERROR: Trying to get property of non-object on controller laravel - laravel

can someone help me with this query why i am getting this error .
hi,
i can't fix the query to set the subject of a mail to be one from db field after $repair->save() in controller
this one works:
Mail::raw($body, function ($message) use ($user, $repair) {
$message->from(Setting::where('setting_key', 'company_email')->first()->setting_value, Setting::where('setting_key', 'company_name')->first()->setting_value);
$message->to('mail#mail.com');
$message->setContentType('text/html');
$message->setSubject(CustomFieldMeta::where('category', 'repairs')->where('parent_id', 108)->where('custom_field_id', 1)->first()->name);
});
or this one:
Mail::raw($body, function ($message) use ($user, $repair) {
$message->from(Setting::where('setting_key', 'company_email')->first()->setting_value, Setting::where('setting_key', 'company_name')->first()->setting_value);
$message->to('mail#mail.com');
$message->setContentType('text/html');
$message->setSubject($repair->id);
});
and this one witch i need is not working:
Mail::raw($body, function ($message) use ($user, $repair) {
$message->from(Setting::where('setting_key', 'company_email')->first()->setting_value, Setting::where('setting_key', 'company_name')->first()->setting_value);
$message->to('mail#mail.com');
$message->setContentType('text/html');
$message->setSubject(CustomFieldMeta::where('category', 'repairs')->where('parent_id', $repair->id)->where('custom_field_id', 1)->first()->name);
});
error: at HandleExceptions->handleError('8', 'Trying to get property of non-object', '/public_html/app/Http/Controllers/RepairController.php', '188', array('message' => object(Message), 'user' => object(User), 'repair' => object(Repair))) in RepairController.php line 188
Thank you.

Related

Laravel Jetstream Route Test With Inertia Returns Error Code 500

Out the test it works, I can visit the page and the controller wroks fine. I wrote the following test:
public function test_logged_user_is_not_redirected()
{
PartnerFactory::new()->create();
$request = $this->actingAs(UserFactory::new()->create())
->get('partners')
->assertRedirect('partners');
dd($request->inertiaProps());
}
I get error code 500. This is the controller:
public function index()
{
return Inertia::render('Partners/Index', [
'filters' => \Illuminate\Support\Facades\Request::all($this->getFilters()),
'contacts' => function() {
return $this->getAllContacts();
}
]);
}
This is the route in web.php
Route::get('partners', [PartnersController::class, 'index'])
->name('partners')
->middleware('auth');
Using refresh database, tried url with a '/' before, I still get 500.
edit: without exception handling i get: Trying to get property 'id' of non-object
Found the solution: The user in jetstream MUST have the personal team!

Laravel - Status Code: 500 Internal Server Error

hello i want to update my data in DB using V-form but 500 internal server error show:
this is my function in controller:
public function update(Request $request, $id)
{ $data =$request->all();
//
$client = Client::where('id', $data['client_id'])->first();
DB::table('projets')->where('id',$id)->update(['name'=>$data['name'],'durre'=>$data['durre'],'description'=>$data['description'],'owner'=>$client->name,'budget'=>$data['budget']]);
}
and this is my route:
Route::apiResource('projet' ,'API\ProjetController');
and this is the vue code:
modifier(){
this.form.put('api/projet/'+ this.form.id).then(function(){
$('#AjouterProjet').modal('hide')
seww.fire(
'Modifier!',
'Your User has been Updated.',
'success'
)
fire.$emit('ajoutprojet');
}).catch(function(){
})
},
In your route specify the Controller function to execute like this
Route::apiResource('projet' ,'API\ProjetController#update');
When you get 500 error, Check the last file on storage/logs directory and try to find the last error on this file, errors are in this format:
[date_time] [error_message] [stacktrace]

Laravel Closure Request

Assalamualaikum guys, i have some problems with my code. i tried to make search by using request parameters on laravel. and here is my code views.
$maintenance->with([
'user_post' => function($query, Request $request){
if($request->has('searchBy') && $request->has('searchQuery'))
{
$parse_query = explode('.',$request->query('searchBy'));
$is_user = $parse_query[0] == 'user_post'? true : false;
if($is_user)
{
$query->where($parse_query[1],'LIKE','%'.$request->query('searchQuery').'%');
}
}
},
'dt_project'
]);
when i tried to execute on browser, it return
1/1) ErrorException
Undefined variable: request
what should i do? thanks to the moon and back. hehehe
try this, change your line 2 to:
'user_post' => function($query) use($request){
explanation:
Request $request can only be used in controller functions like:
public function store(Request $request)
{
//code here
}
in order to pass the $request parameter inside another function, use use (this is also known as variable inheriting):
public function store(Request $request)
{
$user= User::with(['roles' => function($query) use($request){
^^^
$query->where('roles.id', $request['role_id'])->first();
}])->get();
}

laravel same function for send two mail to two user with different view page

In laravel cron i have a function like given below :
public function booking_mail()
{
$data_to_mail= DB::table('tbl_booking as book')
->select('book.id as book_id','book.*','twd.id as wk_id','twd.*')
->join('tbl_workers_details as twd', 'twd.id', '=', 'book.worker_id')
->where('book.status','=','0')
->get();
$data['viewpage']='mailtemplates.booking';
$data['toemail']=$agent[0]->email;
$data['listing_no']=$data_to_mail[0]->listing_no;
$data['cv_no']=$data_to_mail[0]->cv_no;
$mail= Mail::send($data['viewpage'], ['userdata'=>$data], function ($message)
use ($data) {
$message->to($data['toemail'],'Booking Mail')->subject('Inquiry Mail For Booking');
if($data['attach']!=''){
$message->attach($data['attach']);
}
});
if($data['attach']!=''){
unlink($data_to_mail[0]->civil_id_copy);
}
$result=DB::table('tbl_booking')
->where('id','=',$data_to_mail[0]->book_id)
->update(array(
'status'=>'1',
));
}
This function is working fine but when i added one for mail function
at the end of the function its not working returning me the error. i
dont know why this happening to me. i want to do this because i want
to send the mail for two different user with two different data and
view code is given below which is returning me the error.
public function booking_mail()
{
$data_to_mail= DB::table('tbl_booking as book')
->select('book.id as book_id','book.*','twd.id as wk_id','twd.*')
->join('tbl_workers_details as twd', 'twd.id', '=', 'book.worker_id')
->where('book.status','=','0')
->get();
$data['user_viewpage']='mailtemplates.enduser_booking';
$data['toemail']=$agent[0]->email;
$data['listing_no']=$data_to_mail[0]->listing_no;
$data['cv_no']=$data_to_mail[0]->cv_no;
//send e-mail to the agent for booking
$mail= Mail::send($data['viewpage'], ['userdata'=>$data], function ($message)
use ($data) {
$message->to($data['toemail'],'Booking Mail')->subject('Inquiry Mail For Booking');
if($data['attach']!=''){
$message->attach($data['attach']);
}
});
$mail= Mail::send($data['user_viewpage'], ['userdata'=>$data], function ($message)
use ($data) {
$message->to($data['toemail'],'Booking Mail')->subject('Confirmation mail');
});
$result=DB::table('tbl_booking')
->where('id','=',$data_to_mail[0]->book_id)
->update(array(
'status'=>'1',
));
}
why this is happening its returning me the error like:
Swift_TransportException in StreamBuffer.php line 268: Connection could not be established with host mail.XXXX.com [Connection timed out #110]
You have undefined $data["user_viewpage"].

Laravel 5.5 - Send Email - Can't Get Property On Non-Object

I am trying to send email from a Laravel 5.5 controller like this...
$user = User::find(1)->toArray();
Mail::send('emails.invite', $user, function($message) use ($user) {
$message->to($user->email);
$message->from('me#example.com');
$message->subject('Test Subject');
});
This fails with the error...
"message": "Trying to get property of non-object",
If I echo the array into the subject I can see that I do have the correct $user available to me, but for some reason it doesn't like it when I try and extract $user->email
Anyone any ideas?
You're calling ->toArray() on the User object and so it is no longer an object!
$user = User::find(1); // Assuming you are already protecting yourself from potentially not finding a user
$viewData = $user->toArray(); // Or better still don't expose the underlying structure
Mail::send('emails.invite', $viewData, function($message) use ($user) {
$message->to($user->email);
$message->from('me#example.com');
$message->subject('Test Subject');
});
Your $user is type ofarray not object as you expected, because of toArray() called on returned model. Either change that line and remove said toArray() to keep it unchanged object, or make this line:
$message->to($user->email);
look more like:
$message->to($user['email']);
with proper array element reference.

Resources