phplaravellaravel-4

Laravel: InsertgetId and display results


I would like to insert some records into my DB table and using the insertgetid feature, return those results to my blade view.

Controller

$grids[] = array();
        
foreach($c as $key) {
    $grids[] = DB::table('infile')->insertGetId(
            array(  'href' => $key, 
                    'creator_id' => $id,
                    'random' => substr(str_shuffle("aBcEeFgHiJkLmNoPqRstUvWxYz0123456789"),0, 9))
        );
}
        
$name[] = array();
foreach($grids as $id){
    $name = DB::table('infile')->where('id', '=', $id)->first();
}

return View::make('Home')->withName($name);

Blade View

@if(isset($name) && $name != '')
    {{dd($name)}}
@endif

I'm getting this error

ErrorException

preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

Solution

  • You can use whereIn to make exact query. between should work, but it's error prone, since there might be another row inserted in the meantime:

    $ids = [];
    
    foreach (..)
    {
       $ids[] = DB::table('infile')->insertGetId(...);
    }
    
    $data = DB::table('infile')->whereIn('id', $ids)->get();