Support the ongoing development of Laravel.io →
Eloquent Views
Last updated 2 years ago.
0

What is returned from

$annonce = Annonce::find($id);
Last updated 2 years ago.
0

Hey jarektkaczyk

I just did a var_dump($id), and it returns the id correctly, if that is what you mean?

string '111' (length=3)
Last updated 2 years ago.
0

No, I meant the query you make. So dd($annonce);

Last updated 2 years ago.
0

It returns null?

Last updated 2 years ago.
0

Eloquent's find method returns null when there is no record found. You sure there is a record with a primary key of 111?

<h2>{{ $annonce->title }}</h2>

Since $annonce is null, you can't get the title property.

Trying to get property of non-object (View:
/var/www/app/views/annoncer/vis.blade.php)
Last updated 2 years ago.
0

Hey jlaswell,

Now i tried my different articles (annoncer), and none of them will show with the ->title. It finds the article just fine, since it loads the page, with the title of the article in the url, but it returns null when i try to show the ->title within the view.

The weird thing is that this works just fine in my other view?

Last updated 2 years ago.
0

It seems you don't get anything from this query, so the problem is right here. I don't know how you create your url with title, but you should compare the two methods and you will find the bug.

Also you can try

Annonce::findOrFail($id);

then you will get exception if eloquent doesn't find the record

Last updated 2 years ago.
0

You are unfortunately right about the not-found record.

Illuminate \ Database \ Eloquent \ ModelNotFoundException
No query results for model [Annonce].

I know this is hard to answer, but is there any easy way to figure out where the bug lies? Would it help to post my model(s) here?

Thanks!

Last updated 2 years ago.
0

Reached said: I know this is hard to answer, but is there any easy way to figure out where the bug lies? Would it help to post my model(s) here?

I would actually like to see the view and model if you could. Use the Pastebin if you don't mind! :)

Last updated 2 years ago.
0

Let me know if you need any more info than this:

http://laravel.io/bin/nOGa

And thanks again! :)

Last updated 2 years ago.
0

I don't think either of these files is where your problem lies, but I did notice this.

public function annoncer()
{
    return $this->hasMany('Annonce', 'User');
}

The hasMany method has the following signature:

return $this->hasMany('Model', 'foreign_key', 'local_key');

So the above may need to be:

public function annoncer()
{
    return $this->hasMany('User');
}

Fix or explain that and let's see if you still have the error.

Last updated 2 years ago.
0

I tried what you suggested above, however the error still persists. It's getting quite weird now, since I can easily fetch the data from that table in other views, but for some reason it won't let me use it this way with the $id?

Could it be the link to the page that is broken? Since it does have another variable called $value in it, instead of $annonce?

<a href="{{ URL::to('annoncer/' . $value->title) }}">{{ $value->title }}</a>
Last updated 2 years ago.
0

Reached, it seems that you try to create a relation with the model itself here:

public function annoncer()
{
  return $this->hasMany('Annonce', 'User');
}

I suppose you did that to inverse the relation with user, which should be done in User model.

Also you shouldn't use both fillable and guarded properties as the are whitelist and blacklist, so one is redundant.

However none of these is the problem with retrieving particular Annonce. It looks like your query can't find the record in your database, thats the case I think.

Try this:

public function show($id)
{
    $annonce = Annonce::find($id);
    dd(DB::getQueryLog());

    return View::make('annoncer.vis')
        ->with('annonce', $annonce);
}

it will dump your query, then check if the query returns anything from db at all (run it in mysql cli, phpmyadmin or whatever you use for db management)

Last updated 2 years ago.
0

Hey again Jarek,

This is the result of the above:

'select * from `annonces` where `id` = ? limit 1'

But this returns an error in phpmyadmin when i try to run it as SQL, what is the correct syntax here?.

I removed the $guarded from my model, so now my models look like so:

Annonce.php:

<?php

class Annonce extends Eloquent
{
	protected $table = 'annonces';
	protected $fillable = array('title', 'body', 'photo', 'invest_need');

	public function user()
	{
	return $this->belongsTo('User','post_author');
	}
	
    public function annoncer()
    {
        return $this->hasMany('Annonce', 'User');
    }
}

User.php:

<?php

use Zizaco\Confide\ConfideUser;
use Zizaco\Confide\Confide;

class User extends ConfideUser {

public function annoncer()
{
	return $this->hasMany('User');
}

 public function getUserByUsername( $username )
{
    return $this->where('username', '=', $username)->first();
}

}

I have a feeling that my models are messed up, and that is causing my issue..

Last updated 2 years ago.
0

I just tried running the following query in my DB:

SELECT * FROM annonces WHERE id = 112

And it returns the 'annonce' with the corresponding id correctly?

Last updated 2 years ago.
0

The first query is not a valid SQL because it's comes from PDO with bindings.

You say you have no problem in other views, but it doesn't matter, since you can't get data from DB. So maybe show whole controller that is responsible for the views where everything work fine.

And what is the url that you hit for this route?

Last updated 2 years ago.
0

This is the url that im hitting:

<h2 class="annonce-title"><a href="{{ URL::to('annoncer/' . $value->title) }}">{{ $value->title }}</a></h2>

And this is my AnnonceController (pastebin inserted)

http://laravel.io/bin/wrrK

Last updated 2 years ago.
0

Anyone with input as to how I might solve this? :)

Last updated 2 years ago.
0

With debugging non-syntactic errors it's either the logic, data, or you're working on the wrong file.

Last updated 2 years ago.
0

Hi,

I just want to ask in this function,

public function annoncer()
    {
        return $this->hasMany('Annonce', 'User');
    }

The parameters of hasMany are those Models? If it is, I did not it can be done.

Last updated 2 years ago.
0

Hey johnvic,

So you think I should rewrite this part or?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Reached reached Joined 27 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.