I have inserted value in database from the Model
, here is the code:
Controller:
class MovieController extends Controller
{
/**
* Create a new movie model instance.
*
* @return void
*/
public function __construct(MovieModel $movie){
$this->movie = $movie;
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(MovieRequest $request)
{
if($this->movie->createMovie($input)){
return Redirect::back()->with('message', '<strong>' . $input['name'] . '</strong> movie has been created');
}
}
}
Model:
class Movie extends Model
{
public function createMovie($input) {
return $this->insert($input); // I need to return last inserted id here
}
}
By using this insert method how can I get the last inserted id?
If your table has autoincrementid
field then Try using insertGetId
function which will return last inserted row id
return DB::table('movie')->insertGetId(
//pass an array parameter to this function
);
In your model, add this code:
public function createMovie($input) {
$id = $this->insertGetId(
$input
);
return $id;
}
See, if that helps.
Try this:
class Movie extends Model
{
public function createMovie($input) {
$insertedId = $this->insert($input); // I need to return last inserted id here
$theId = (int) $inserted->id;
return $theId;
}
}
This suppose that '$this->insert($input);' is the query you are executing.
To give you the idea you get the id directly fron your executed query:
$qry = $this->movie->createMovie($input)
$insertedId = $qry->id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.