Home>
I have the following relationship model.
BookUser (one) to Book (many).
So, when I want to make a list function of a book, I want a controller to create a query including a free book by creating a query, so I want to change the contents such as orderBy in ajax.
But what I want now is different from what you actually want. The book I borrowed is not out,
public function index (BookIndexRequest $request)
{
$query = Book :: select ('books. *')
->john ('bookUsers', 'bookUsers.id', '=', 'books.borrowing_user_id');
if ($request->ajax ()) {
if (! empty ($request->get ('seachText'))) {
$query->where ('book.name', 'LIKE', '%'. $request->get ('seachText'). '%');
}
if (! empty ($request->get ('orderBys'))) {
foreach ($request->get ('orderBys') as $key =>$value) {
$query->orderBy ($key, $value);
}
}
return \ Response :: json ($query->get ());
}
$books = $query->get ();
return view ('books.index', compact ('books'));
}
/ **
query Contents
books.book_name | books.borrowing_user_id | bookUsers.name
BookA | 1 | Tom
BookC | 5 | Peter
*/
This is what I really want
books.book_name | books.borrowing_user_id | bookUsers.name
BookA | 1 | Tom
BookB | Null | Null
BookC | 5 | Peter
BookD | Null | Null
BookE | Null | Null
BookF | Null | Null
BookG | 11 | Ken
Teach us how to create a query with the content I want.
Applicable source codeBookUser model
class BookUser extends Model
{
use SoftDeletes;
protected $fillable = [
'name'
, 'created_at'
, 'updated_at'
, 'deleted_at'
];
}
public function books ()
{
return $this->hasMany (Book :: class, 'borrowing_user_id', 'id');
}
Book model
class Book extends Model
{
use SoftDeletes;
protected $fillable = [
'borrowing_user_id' // available when null
, 'book_name'
, 'created_at'
, 'updated_at'
, 'deleted_at'
];
}
public function borrowing_user ()
{
return $this->belongsTo (User :: class, 'borrowing_user_id', 'id');
}
Supplemental information (FW/tool version etc.)
Laravel 5.7
PHP 7.2
CentOS 7
-
Answer # 1
Related articles
- about creating a controller for laraveladmin
- php - about the language for creating anonymous bulletin boards
- unity - about the idea of creating a 4-choice quiz
- php - about laravel: problems with unit testing (database testing)
- php - [laravel] questions about eloquent optimization
- c # - about creating a combo box that allows partial match search
- php - about overriding the laravel controller method
- javascript - about processing when you want to pass a query to the url at the time of page transition on the website
- php - about laravel eloquent
- php - about the scope of laravel variables
- ruby on rails 5 - about the structure when creating a blog with rails 5
- (re-question) about creating multiple records for access queries
- about laravel form submission
- about creating multiple records for access queries
- python - about the error that occurred when creating the table
- about languages and frameworks for creating gui apps for windows
- html - about creating a pull-down menu
- ruby - about permission setting when creating rails application
- about laravel's eloquent
- about building laravel environment
Trends
Controller
Model