when I try to add my second data after my very first data that has been added this error shows Call to a member function genres() on null
. and here is a image of the error (See image error here).
Controller - here is where the error persists after clicking the submit button.
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|max:190',
'director' => 'required|max:190',
'content_rating' => 'required',
'category' => 'required',
'ewallet_price' => 'required|regex:/^\d*(\.\d{1,2})?$/|max:190',
// 'token_price' => 'required|regex:/^\d*(\.\d{1,2})?$/|max:190',
'running_time' => 'required',
'release_date' => 'required',
'cast' => 'required|max:190',
'genres' => 'required',
'cover_image' => 'nullable|image|mimes:jpeg,jpg,png',
'movie_video' => 'mimetypes:video/x-ms-asf,video/x-flv,video/mp4,application/x-mpegURL,video/MP2T,video/3gpp,video/quicktime,video/x-msvideo,video/x-ms-wmv,video/avi|required',
'trailer_video' => 'mimetypes:video/x-ms-asf,video/x-flv,video/mp4,application/x-mpegURL,video/MP2T,video/3gpp,video/quicktime,video/x-msvideo,video/x-ms-wmv,video/avi|required',
'movie_description' => 'required',
'clients' => 'required'
]);
//Handle File Cover Image
if($request->hasFile('cover_image')){
//Get filename with extension
$filenameWithExt = $request->file('cover_image')->getClientOriginalName();
//Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
//Get just ext
$extension = $request->file('cover_image')->getClientOriginalExtension();
//Clean filename (Replace white spaces with hyphens)
$cleanFilename = str_replace(' ', '-', $filename);
//Cleaner filename
$cleanerFilename = preg_replace('/-+/', '-', $cleanFilename);
//Filename to store
$fileNameToStore = $cleanerFilename.'_'.time().'.'.$extension;
//Upload image
$path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
//Handle File Movie
if($request->hasFile('movie_video')){
//Get filename with extension
$filenameWithExt = $request->file('movie_video')->getClientOriginalName();
//Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
//Get just ext
$extension = $request->file('movie_video')->getClientOriginalExtension();
//Clean filename (Replace white spaces with hyphens)
$cleanFilename = str_replace(' ', '-', $filename);
//Cleaner filename
$cleanerFilename = preg_replace('/-+/', '-', $cleanFilename);
//Filename to store
$fileNameToStoreVid = $cleanerFilename.'_'.time().'.'.$extension;
//Upload image
$path = $request->file('movie_video')->storeAs('public/movie_videos', $fileNameToStoreVid);
} else {
$fileNameToStoreVid = 'novideo.png';
}
//Handle File Trailer
if($request->hasFile('trailer_video')){
//Get filename with extension
$filenameWithExt = $request->file('trailer_video')->getClientOriginalName();
//Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
//Get just ext
$extension = $request->file('trailer_video')->getClientOriginalExtension();
//Clean filename (Replace white spaces with hyphens)
$cleanFilename = str_replace(' ', '-', $filename);
//Cleaner filename
$cleanerFilename = preg_replace('/-+/', '-', $cleanFilename);
//Filename to store
$fileNameToStoreVidTrailer = $cleanerFilename.'_'.time().'.'.$extension;
//Upload image
$path = $request->file('trailer_video')->storeAs('public/trailer_videos', $fileNameToStoreVidTrailer);
} else {
$fileNameToStoreVid = 'novideo.png';
}
//Create Movie
$movie = new Movie;
$movie->title = $request->input('title');
$movie->movie_description = $request->input('movie_description');
$movie->cast = $request->input('cast');
$movie->content_rating = $request->input('content_rating');
$movie->director = $request->input('director');
$movie->category_id = $request->input('category');
$movie->ewallet_price = number_format($request->input('ewallet_price'));
$movie->token_price = 10;
$movie->running_time = $request->input('running_time');
$movie->release_date = $request->input('release_date');
$movie->movie_provider = $request->input('clients');
$movie->cover_image = $fileNameToStore;
$movie->movie_video = $fileNameToStoreVid;
$movie->trailer_video = $fileNameToStoreVidTrailer;
$movie->save();
$genres = $request->input('genres');
foreach($genres as $genre){
$movie
->genres()
->attach(Genre::where('name', $genre)->first());
}
return redirect('/admin/movies')->with('success', 'Movie Uploaded');
}
that is when i store the data and here is my vie
View - here is my view where all the input is coded and forms.
{!! Form::open(['action' => 'Admin\MoviesController@storeCategory', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<div class="card" style="width: 100%;">
<div class="card-header">
Manage Categories
</div>
<br>
<div style="width: 100%; padding-left: -10px; border: 1px;" class="">
<div class="table-responsive">
<table id="categories-table" class="table table-striped table-hover dt-responsive display cell-border" cellspacing="0">
<thead>
<tr>
<th>#</th>
<th>Category</th>
<th>E-Wallet Price</th>
<th>Token Price</th>
<th>Description</th>
<th>Created At</th>
<th>Updated At</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@if(count($categories))
@foreach($categories as $category)
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<div class="row">
<a href="/admin/movies//editCategory" class="btn btn-sm btn-primary" ><span data-feather="edit"></span></a>
{!!Form::open(['action' => ['Admin\MoviesController@destroyCategory', $category->id], 'method'
=> 'POST', 'class' => 'float-right'])!!} {!!Form::close()!!}
</div>
</td>
</tr>
@endforeach
@else
<p>No Categories Found</p>
@endif
</tbody>
</table>
</div>
</div>
</div>
<br><hr> <h2>Create a Movie Category</h2> <br>
<div class="card" style="width: 100%;">
<div class="card-header">
Category
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item center text-center">
<div class="form-group row">
<div class="col-sm-7">
</div>
</div>
</li>
<li class="list-group-item center text-center">
<div class="form-group row">
<div class="col-sm-7">
</div>
</div>
</li>
<li class="list-group-item center text-center">
<div class="form-group row">
<div class="col-sm-7">
</div>
</div>
</li>
<li class="list-group-item center text-center">
<div class="form-group row">
<div class="col-sm-7">
</div>
</div>
</li>
<li class="list-group-item text-center"> {!! Form::close() !!}</li>
</ul>
</div>
My Route
Route::post('admin/movies/storeCategory', 'Admin\MoviesController@storeCategory');
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2TAHkXs
via IFTTT
Aucun commentaire:
Enregistrer un commentaire