Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jaeturma's avatar

Need to add data in two tables

i have downloaded a script running laravel 5.2 and in a process users need to register before adding some list (list <- child table) and this works fine. What I need to do is combine registration together with a 3 lists. Therefore, registration still goes to users table and list goes to list table. Do I need to specify this in model?

I have this code so far.

ListingUserController.php

use Auth;
use App\User;
use App\Listings;
use App\Categories;
use App\SubCategories;
use App\Location;

use App\Http\Requests;
use Illuminate\Http\Request;
use Session;
use Intervention\Image\Facades\Image; 
use Illuminate\Support\Facades\DB;

class ListingsUserController extends Controller
{
    public function submit_listing()    { 

    $categories = Categories::orderBy('category_name')->get();
    $locations = Location::orderBy('location_name')->get();
    return view('pages.addeditlisting',compact('categories','locations'));
}

public function addnew(Request $request)
{   
    $data =  \Input::except(array('_token')) ;
    $rule=array(
            'category' => 'required',
            'sub_category' => 'required',               
            'title' => 'required'
             );
    
    $validator = \Validator::make($data,$rule);
    if ($validator->fails())
    {
            return redirect()->back()->withErrors($validator->messages());
    } 
    $inputs = $request->all();
    
    if(!empty($inputs['id'])){
        $listings = Listings::findOrFail($inputs['id']);
    }else{
        $listings = new Listings;
    }
    
    $listing_slug = str_slug($inputs['title'], "-");
    
    if(empty($inputs['id'])){ 
        $listings->user_id = Auth::User()->id;
    } 

    $listings->cat_id = $inputs['category'];
    $listings->sub_cat_id = $inputs['sub_category'];
    $listings->location_id = $inputs['location']; 
      
    $listings->save();

    if(!empty($inputs['id'])){
        \Session::flash('flash_message', 'Changes Saved');
        return \Redirect::back();
    }else{
        \Session::flash('flash_message', 'Listing Added');
        return \Redirect::back();
    }                  
     
}     

IndexController.php

namespace App\Http\Controllers;

use Auth;
use App\User;
use App\Categories;
use App\Listings;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Intervention\Image\Facades\Image; 

class IndexController extends Controller 
{

public function register()
{            
    return view('pages.register');
}

public function postRegister(Request $request)
{ 
    
    $data =  \Input::except(array('_token')) ;
    
    $inputs = $request->all();
    
    $rule=array(
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => 'required|email|max:75|unique:users',
            'password' => 'required|min:3|confirmed'
             );
    
     $validator = \Validator::make($data,$rule);
 
    if ($validator->fails())
    {
            return redirect()->back()->withErrors($validator->messages());
    } 
           
    $user = new User;

$user->first_name = $inputs['first_name'];  
$user->last_name = $inputs['last_name'];
    $user->email = $inputs['email'];     
    $user->password= bcrypt($inputs['password']); 
       
    $user->save();
        
        \Session::flash('flash_message', '<b>Register successfully!');

        return \Redirect::back();
}    

Any help would greatly appreciated and helps me a lot.

0 likes
18 replies
bobbybouwmann's avatar

So you said you downloaded a script and now you want to adjust it right? So basically you have no idea what the code is doing and you suspect us to help you out! I can be wrong here, but I'm not really into helping someone that isn't even trying!

Adding data to a second table is the same as inserting it in the first table. In above code you already fill both tables, so you already have the code! That also makes me think that you have no clue at this point!

Try the documentation: https://laravel.com/docs/5.6

bashy's avatar

That code looks very Laravel 4.2ish :P

rin4ik's avatar

@bashy I'm wondering how do you earn experience points without best reply awards?

Cronix's avatar

@bashy I'm wondering how you earn experience points without best reply awards?

@rin4ik What do you mean? @bashy has 560 best reply awards? You also get xp by posts, replies, watching videos, having someone like your post, etc.

Also, what does this have to do with the question?

rin4ik's avatar

@Cronix his XP magically growing. I know it isn't the place to discuss it sorry

bashy's avatar

@rin4ik I'm not keeping track but I just post replies and stuff. Are you stalking? :P

jaeturma's avatar

@bashy i have this in composer.json

    "require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.2.*",

does doesn't mean its 5.2?

@bobbybouwmann i have already modified a lot on this script. i know simple things going on so far. i just want to try making the script more friendly. I know eloquent is the right thing but i want to look into what it is existing. that is why i posted here guys just thinking if somebody can help me.

bashy's avatar

@rin4ik I've been watching videos just now? Although I've been talking to Bobby about our replies being liked a lot.

And it appears to be the same for Bobby. 50 odd best replies since last scoreboard cache.

rin4ik's avatar

sorry about that @bobbybouwmann . but I know the full story @bashy since last month I noticed that when @bobbybouwmann actively participates in threads and earns best reply awards by helping others your xp grew all time . after I decided write you about it in this thread, because it was very strange to me. and when m-rk posted about xss vulnerability in this forum I totally understand your way of getting xp. I didn't know how can I prove that. and I thought about increasing @bobbybouwmann's best reply awards to test your activity when he reach 1 place. I have here question which answered @bobbybouwmann https://laracasts.com/discuss/channels/eloquent/how-can-i-get-rid-of-n1-problem … . and start marking his replies as best again and again.( I'd like this been fixed as well because laracasts big community and anybody can be in 1 place within 1 day by increasing best reply awards ) and you also start increasing your points.

bashy's avatar

@rin4ik You have been here 11 months? We have been here 4 years helping people on these forums. We received t-shirts when we were the first people to reach 500k and that was 3 years ago now. You've been faking his best answers and now he has no way of knowing how many he's actually got. I haven't used fake best replies and I'm not sure what XSS thread has to do with this?

Cronix's avatar

I'm sure @jaeturma appreciates the help. Why didn't you just start a new thread raising this issue instead of hijacking this one @rin4ik ?

jaeturma's avatar

I just thought this was the right place to get help. So sad for the others they only use posts to gain XP. Anyway guys thanx for the help. I went back to traditional procedural PHP since it was a rush. Anyway, I can learn it later the extra time I have.

Please or to participate in this conversation.