Laravel Join Query gives Trying to get property of non-object - laravel-4

I get results with join query but in view when I want to show it, it says: trying to get property of non object. I am working over a simple blogging system and I am trying to get blogs from following friends. Here is my view:
#foreach($blogposts as $posts)
<h4>{{ $posts->title }}</h4>
#endforeach
I am sure the problem is at view but I added my controller too, maybe needed and here is my controller:
$posts=DB::table('following')
->join('page_posts', 'following.p_id', '=', 'page_posts.p_id')
->select('page_posts.*')
->where('u_id','=',Auth::User()->id)
->orderBy('updated_at','desc')
->get();
return View::make('index')->with('blogposts',$posts);
Help me with this please.

Try this. You need to pass the values as like this.
return View::make('index',array('blogposts'=>$posts));

I should have used eloquent to solve this means that my controller should be like this:
$posts=following::join('page_posts', 'following.p_id', '=', 'page_posts.p_id')
->select('page_posts.*')
->where('u_id','=',Auth::User()->id)
->orderBy('updated_at','desc')
->get();
return View::make('index')->with('blogposts',$posts);
The problem solved. I posted the answer, because someone else maybe needs.

Related

Laravel / Lumen $users = \App\User::find$Uid)->get();

normally i use this:
$users = \App\Users::where('age', '>=', '20')->paginate(20);
But if I use raw querys or use the get(); method from querybuilder, I got an array not a collection or a paginator instance as result.
How can I get my ->links(); from that array result?
Hope all is explained well. :-)
KR,
Marcel
Write{{ $users->links() }} in your view page. According to your query, pagination will show while your data return more than 20 rows.
For better understand Read this doc

Laravel 5.5 - Deep Relations calling by -->

I now spent hours googling and experimenting on trying to get an relation with two intermediate tables working.
My database looks like this:
(apt_id is apartment_id in real, was shorter to write)
I have every relation one away setup correctly with belongsTo and and hasMany:
EXAMPLE FROM House.php
public function user()
{
return $this->belongsTo('App\User');
}
public function apartments()
{
return $this->hasMany('App\Apartment');
}
Isn't there a way to access these relations like:
$house->apartments->tenants->entries
in Blade:
#foreach ( $house->apartments->tenants->entries as $entry )
, since I want to display all house entries on house.show (Blade View)
The only way it's working is by using a bunch of foreach inside each others... :/ and they define the order...
Using my wanted relation calling produces:
Property [tenants] does not exist on this collection instance.
displayed on the page.
Greetings,
Pat
I don't think you can achieve what you want using the code you posted, because when calling, for example, $house->apartments it returns a Collection object. So, it is not dealing with database anymore, that's why you would need to use a bunch of #foreachs.
I don't know if this is the best way to solve this, or if it will help you in your actual problem, but you could think this problem backwards and try something like this:
$entries = \App\Entry::whereHas('tenants', function($q) use ($house) {
$q->whereHas('apartments', function($q1) use ($house) {
$q1->where('apartments.house_id', $house->id);
});
})->get();
And in the view:
#foreach ($entries as $entry)
{{ $entry->tenant->apartment->house->name }}
#endforeach

Paginator on laravel 4.2

I am new to laravel and I am trying to get a pagination function into my result pages, so I have the following function to generate results from query and I would like to have a pagination on the results page, but I don't seem to get it work correctly
public function showResults()
{
$selectedquery = Input::get('Annonces');
$what = Input::get('what');
$where = Input::get('where');
$results = DB::table('annonces')->where($selectedquery,'LIKE', '%'.$what.'%')
->where('Lieu','LIKE', '%'.$where.'%')
->get();
return View::make('results',array('results' => $results));
}
Any Help?
Well, for one, you're missing the call to ->paginate(n). Right now, your closure is ->get(), which returns all results for your annonces table. This is good, but doesn't work for pagination. Change the function like so:
$results = DB::table('annonces')->where($selectedquery,'LIKE', '%'.$what.'%')
->where('Lieu','LIKE', '%'.$where.'%')
->paginate(10);
This will return all results grouped into 10 results per page. Feel free to change that as you see fit.
Lastly, somewhere on your view where you display the results, you will need to use this code to display a page-viewer:
<?php echo $results->links(); ?>
<!-- OR -->
{{ $results->links(); }}
Also, be sure to check out the docs on Laravel's pagination. You'll find it's pretty comprehensive!
Laravel Pagination
Hope that helps!

laravel Eloquent ORM - How to get compiled query?

In Laravel 4.2 I want to get Compiled Query.
This is what i have:
$product = Product::where('id', '=', '100')->get();
I want compiled query like:
select * from products where id = 100
Purpose of the question is: i want to use it as sub query in another query.
I have searched and found Class Grammer and Class MySQL But i did not found solution for that.
Is there any solution?
Your help would be appreciated.
The easiest way is probably mostly finding out what query was just executed, rather than what the query will be. Something like this:
function latestQuery()
{
$queries = DB::getQueryLog();
return end($queries);
}
Hopefully that's the same for your purposes.
You can get the SQL query like this:
use DB;//write this at the top of the file above your Class
DB::enableQueryLog();//Enable query logging
$product = Product::where('id', '=', '100')->get();
dd(DB::getQueryLog());//print the SQl query
You can register an event listener in your routes file(in development phase), which will listen for the laravel query event and var_dump the executed query.
Event::listen('illuminate.query', function($sql)
{
var_dump($sql);
});
But this will turn out to be messy. So better use something like Clockwork. It is awesome, you can view all the executed query in your browser.
You can use grammer for subqueries, See below example for reference :
$users = DB::table('users')
->where('user.id', '=', $userID)
->join('product', 'product.userid', '=', 'user.id');
$price = $users->select(array(DB::raw('SUM(price)')))
->grammar
->select($users); // Compiles the statement
$result = DB::table('users')->select(array(DB::raw("({$price}) as price)))->get();
This add a subquery to your main query.

update record with codeigniter active record

In my controller, I am calling a model function with the following:
$this->sales_model->softdelete_order($this->input->post('ordernumber'));
what I want to do, in my model is
update Customer_Order_Summary set Deleted='1' where CustomerOrderID='123'
where 123 is $this->input->post('ordernumber')
My Model syntax is:
function softdelete_order($q){
$this->db->set('Deleted','1');
$this->db->where('CustomerOrderID', $q);
$query = $this->db->update('Customer_Order_Summary');
}
This is not working or outputting any errors.
The model is preloaded and the post information is posting and echoing correctly so purely a model syntax issue I think.
Help appreciated as always.
Thanks,
After
$query = $this->db->update('Customer_Order_Summary');
add
$this->db->last_query();
to see your query and you can repair it from there.
Try using
$this->db->set('Deleted',1);
If not working post your exact error

Resources