Here I'm trying to prevent duplicate values in my DB. But unexpectedly I got this error. Moreover author_id is default null. even I'm unable to throw an exception. Any help that would be a great help for me.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use DB;
use App\Models\Book;
use App\Models\Author;
class BooksController extends Controller
{
public function store(Request $request)
{
$input = Input::all();
$book = new Book;
$book->id = Input::get('id');
$book->title = Input::get('title');
$book->author->id = Input::get('author_id');
$book->price = Input::get('price');
$book->isbn = Input::get('isbn');
$book->language = Input::get('language');
$book->isbn = Input::get('isbn');
$book->year_of_publisher = Input::get('year_of_publisher');
if(DB::table('books')->where('title', '=', Input::get('title'))->get()){
return 'Book is already there';
} else{
$book->save();
return $book; }
}
public function bookFormat($book){
return [
'Id' => $book->id,
'Title' => $book->title,
'Author' => [
'Id' => $book->author->id,
'First_name' => $book->author->first_name,
'Last_name' => $book->author->last_name,
'Age' => $book->author->age,
'Country' => $book->author->country
],
'Price' => $book->price,
'ISBN' => $book->isbn,
'Language' => $book->language,
'Year' => $book->year_of_publisher
];
}
}
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2mhxHBt
via IFTTT
Aucun commentaire:
Enregistrer un commentaire