Ad
Many To Many Retreiving Book & Author Name Error Laravel
Unable to display all books name and author name as list. Getting error. If I return the books and authors variables in controller (index method) I get the data as array. But unable to access them in view. Also as mentioned in commented code (in Book Controller) below, code from laravel documentation shows error.
Book Model
class Book extends Model
{
protected $guarded = [];
public function authors(){
return $this->belongsToMany('App\Models\Author')->withTimestamps();
}
}
Author Model
class Author extends Model
{
public $guarded = [];
public function books(){
return $this->belongsToMany('App\Models\Book')->withTimestamps();
}
}
Book Controller
public function index()
{
$query = Book::with('authors')->get();
Pls Read the commented lines below -
//foreach ($query as $book) {
//return $book->authors->author_name; //Error: Property [author_name] does not exist on this collection instance.
//}
// (above code is given in laravel documentation but shows error : why?)
foreach($query as $b){
$books[] = $b->book_name;
$authors[] = $b->authors->first()->author_name;
}
return view('books.index', compact('books','authors'));
}
public function create()
{
return view('books.create');
}
public function store(validateAuthorBook $request)
{
$validated = $request->validated();
if($validated){
$book = new Book();
$book->book_name = $request->book_name;
$book->save();
$author = new Author();
$author->author_name = $request->author_name;
$author->save();
$book->authors()->attach($author);
}
return back()->with('status', 'Insert Successful!');
}
View (index.blade.php)
@foreach($books as $b)
<tr>
<th>{{$loop->iteration}}</th>
<td>{{$b->book_name}}</td>
//<td>how to get author name</td>
@endforeach
</tr>
error
Facade\Ignition\Exceptions\ViewException
Trying to get property 'book_name' of non-object (View:
D:\ProgrammingSSD\laragon\www\ulclibrary\resources\views\books\index.blade.php)
Ad
Answer
You are getting error because you are making $book
a string in array ($books[] = $b->book_name;
). You shouldn't do anything with $books
collection in controller, and could send it directly to view.
Controller:
public function index()
{
$books = Book::with('authors')->get();
return view('books.index', compact('books'));
}
View (one line, split authors with comma):
@foreach($books as $book)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $book->book_name }}</td>
<td>{{ $book->authors->pluck('author_name')->implode(', ') }}</td>
</tr>
@endforeach
View (loop, split authors with new line):
@foreach($books as $book)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $book->book_name }}</td>
<td>
@foreach($book->authors as $author)
{{ $author->author_name }}<br/>
@endforeach
</td>
</tr>
@endforeach
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms
Ad