could not access laravel object data - laravel

$cntct = DB::table('products')
->join('merchants','merchants.id','=','products.merchant_id')
->where('products.id',$id)
->get();
dd($cntct[0]->address);die;
data show from this query
"{
"address":"dummy"
"state":"dummy",
"zip":"45345",
"country":"Pakistan",
"city":"dummy"
}"
I want access this address but when i write dd($cntct[0]->address->address);die;
error show
Trying to get property 'address' of non-object

Maybe, change the
->get()
to
->first()
Cuz you only want one value (you give the products id)
And then
dd($cntct->address); // no need of die

You can try foreach ($cntct as $item){ //your code }.
Maybe the query does not return any result and that's the cause of an error - Trying to get property 'address' of non-object.

Related

Laravel View error query Property does not exist on this collection instance

I have this function in my controller :
public function edit(funcionario $item){
$ferias = feria::join ('funcionarios','funcionarios.id','=', 'ferias.id_funcionario')
->where( 'ferias.id_funcionario', '=', $item->id)
->get(['ferias.id_funcionario', 'ferias.n_ferias_disponiveis', 'funcionarios.id']);
return view('painel-admin.utilizadores.edit', ['item' => $item, 'ferias' => $ferias]);
}
The "Item" work perfectly but "ferias" not.
I want to show the values ​​that are inside the vacation field.
strong text
value="{{$ferias}}" This it work and show this :
[{"id_funcionario":23,"n_ferias_disponiveis":22,"id":23}]
but i try this value="{{$ferias->n_ferias_disponiveis}}" show this error :
Property [n_ferias_disponiveis] does not exist on this collection instance
return value in collection, so you have to make a foreach loop to get the particular one field...
foreach($ferias as $feria)
{
$name = $feria->n_ferias_disponiveis;
}

How can I get the laravel join method without using foreach command?

How can I get the laravel join method without using foreach command? How can I solve it without using the Foreach command
{{$app->name}} I used to this type. But I'm constantly getting error.
Controller.php file content
public function show($id)
{
$show = Duty::where('duty_id', '=', $id)->count();
if ($show!=0){
$app = DB::table('users')
->join('duties', 'duties.appointed_user_id', '=', 'users.id')
->select('users.name', 'duties.*')
->get();
$data = Duty::where('duty_id', '=', $id)->get();
return view('duty.show', compact('data', 'app'));
}
else
{
return redirect()->back()->with('status', 'Sorun oluştu');
}
}
Property [name] does not exist on this collection instance. (View: D:\xampp\htdocs\personality\resources\views\duty\show.blade.php)
You have a list (collection) of users, not a single user. That is why you can't get just the name from it, because it doesn't know which one to get the name of. This problem has nothing to do with using the join method.
If you only expect to get one result from your query, you could change from ->get() to ->first(). This would allow you to call {{$app->name}} without breaking. But only do this if you expect a single result and use first.
If you expect more than one user, there is no way to display the names without looping in some way.
That's what relationships are for. In that case you could do User::with(:duties'). Or in your case the other way around would probably work better Duty::find($id)->with('user')

Trying to get property of non-object for just one list on laravel controller?

I only get one list from my DB
$postalls=
DB::table(users)
->select('*')
->where('id','=',5)
->get();
return View('codo.postview')
->with('postalls', $postalls);
when I use below on postview.blade.php
{{$postalls[0]->comment}}
I got the error message
Trying to get property of non-object
I know I can fix the view problem by use
#foreach($postalls as $postall)
{{$postall->comment}}
#endforeach
But I want to ask~
Can I not use the foreach function to fix the error message?
I also try below but I got the same error
Can I transfer the collection to object?
$postalls=
DB::table(users)
->select('*')
->where('id','=',5)
->get()
->first();
{{$postalls->title}}
use first() with or without get()
dd($postalls)
Got the same result~
My id come from the button pushed and sent to the controller
public function postshow($table,$id)
{
$postallsold=
DB::table($table)
->select('*')
->where('id','=',$id)
->get()
->first();
dd($postallsold);
I think you were almost there. Try using first() instead of get().
DB::table('users')
->select('*')
->where('id', '=', 5)
->first();
This should return an instance of StdClass instead of a collection.
If you still get the same error, it could be that there is no record matching ID 5.

database query not working properly laravel

I have a database query as below:
$data = DB::table('settings')->select('value')->where('name', '=', $name)->get();
echo $data["value"];
but it gives error:
Undefined index: value
if I echo $data; then I get below result:
[{"value":"theme_default"}]
get() returns a Laravel Collection object, which has magic methods to turn itself into a string when you try to use it as such (like with an echo statement). As you can see in the JSON that you've printed, $data is an array or collection of objects, so you want the first one in the collection before trying to get the value:
$row = $data->first();
echo $row->value;
Try this:
$data = DB::table('settings')->select('value')->where('name', '=', $name)->first();
echo $data["value"];
//or
echo $data->value;
You can turn the result into an array like this:
$data = DB::table('settings')
->select('value')
->where('name', '=', $name)
->get()
->toArray();
//then use the value of the first element
dd(#$data[0]['value']);
, but I strongly suggest that you use the Laravel Collection in order to make use of its powerful methods.

Laravel - Trying to get property of non-object in chained view

Having a small problem with retrieving a new object on my view. I keep getting the error 'Trying to get property of non-object'.
The field I am trying to display 'oil_gas_skillsgap' is a valid field and when I do a simple return it spits out...
[{"oil_gas_job_id":"4","industry_job_id":"43","oil_gas_skillsgap":"test text here"}]
CONTROLLER
public function showjob($slug)
{
$job = OilGasJob::where("slug", "=", $slug)->first();
if (is_null($job))
{
return Redirect::to('/');
} else {
$jobId = $job->id;
$roleId = Session::get('industryrole');
$skills = DB::table('industry_job_oil_gas_job')
->where('oil_gas_job_id', '=', $jobId)
->where('industry_job_id', '=', $roleId)
->get();
return View::make('job')
->with('job', $job)
->with('skills', $skills);
}
}
VIEW
{{ $job->job_title}}
{{ $skills->oil_gas_skillsgap }}
$skills is probably an array, not an object, even if the query has only one result.
If you know for a fact that your query will return one result, use first() and you'll get it as an object directly.
$skills = DB::table('industry_job_oil_gas_job')
->where('oil_gas_job_id', '=', $jobId)
->where('industry_job_id', '=', $roleId)
->first();
Otherwise, remember the following: when using the DB class for queries, Laravel will always return an array. If you use an Eloquent model, it will return an Illuminate\Support\Collection object. In both cases that is true even if no row is found (the array or Collection will be empty). The first() method, on the other hand, will either return an object (stdClass when using DB, or a model when using Eloquent) or null.
#user3189734,
job_title is not a part of $job collection, that is why it is throwing 'Trying to get property of non-object'.

Resources