Hello, I’m trying to search the Laravel, who can help me

Asked

Viewed 63 times

-2

I have a Model called Record, This Model has a method like HasOne who is called Item, I’m implementing a search, but here’s the problem. All the methods work normally, the problem is the following, when I do the search the results are returned in the two queries, see how they are:

<?php  
$record->where( 'title',  'LIKE', "%{$request->ask}%" );
// essa tabela retorna como foi pedido, um modelo Record com o title e outros campos.

$record->item()->where( 'name',  'LIKE', "%{$request->ask}%" );
// Essa também retorna o que foi pedido um modelo Item com o name e outros campos

What I want is to make the second query return, not only the items table, but also return the parent model with all the methods that exist in the model, so I can use it as the original.

  • The father is the item()?

  • The Record is the father.

  • you want to run the filter on the two?

  • I want to search the Record if the terms exist it returns the Record model, if it exists in the Item model Returns the Record relative to it.

  • I have not already reviewed the ORM no more is there the answer, I think I will have to change my database logic so, more vlw for trying to help me, I think in my heart my logic is that is wrong.

Show 1 more comment

2 answers

0


By commenting:

I want to search the Record if the terms exist it returns the Record model, if it exists in the Item model Returns the Record relative to it.

$ret = $record->with('item')->where('title','LIKE',"%{$request->ask}%")->get();

That is, use with to load the relationships(s);

if it is for model item

$ret = $item->with('record')->where('title','LIKE',"%{$request->ask}%")->get();

More answers:

  • Column not found: 1054 Unknown column 'Breed' in 'Where clause' (SQL: select * from records Where title LIKE %dr% and breed LIKE %dr%)

  • This has nothing to do with your question @Bailedogauchinhom, in your question this has no, we answer what is in the question has no way to guess what you did.

  • is also simple @Bailedogauchinho what are the fields of your model I think that breed does not belong to the records

  • when I did what I said up there I received this error, which says that the column 'Breed' does not exist in the table Records, that is why it only exists in the tables items.

  • Next you did something I do not know, I answered what you asked and said in the comment, remember that if the field belongs the filter relation should be done in the relation. You have a doubt but, could not say what it is. Which expression are you using? is not equal to mine ... but, certainly not equal

  • I don’t think you understand what I wrote up there.

  • Explain @Bailedogauchinho, explain?

  • $record->item()->Where( 'name', 'LIKE', "%{$request->Ask}%" );

  • You want to filter by item and bring the record, in relation?

  • this line returns the item when I do the search, however, from it I want to return the Record model that is the father of this item.

  • You have to do it for item then by model item @Bailedogauchinho did the editing.

  • ball show, I’m very grateful.

Show 7 more comments

0

To call the parent model, (or collection) Voce has to add a ->with(). Remembering that for this to work, a relationship has to be done in the existing model. ex:

$item->with('record');

And in the item model there should be:

public funcion record(){
    return $this->hasMany('NomeDaSuaTabela');
}

The type of relationship can be another too, depends on the self bank, in the documentation have examples of all forms of making these relationshipsless.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.